## Shared Identity and Opioid Addiction
## Michael Hankinson and Justin de Benedictis-Kessner
## Data collected June 2019 via NORC

## This script ingests the recoded data and produces the main analyses and
##    figures for the paper and appendix



#### Preliminary stuff ####
library(foreign)
library(tidyverse)
library(stargazer)
library(svglite)
library(Cairo)
library(janitor)
library(broom)

red_mit = '#A31F34'
red_light = '#A9606C'
blue_mit = '#315485'
grey_light= '#C2C0BF'
grey_dark = '#8A8B8C'
black = '#353132'



## read in recoded dataset:
a <- read_rds("opioids_identity_recoded.rds")

# writeLines(prettyNum(nrow(a),big.mark = ","),"n_overall.tex")


## descriptive statistics table
descriptive_tab <- stargazer(a[,c("female_resp",
																	"white_resp",
																	"black_resp",
																	"age",
																	# "college",
																	# "income_gt50k",
																	"own",
																	"urban",
																	"suburban",
																	"rural",
																	"personal_bi")], 
														 summary = T, digits=2,
														 summary.stat=c("mean","sd","median","min","max","n"), 
														 covariate.labels = c("Female",
														 										 "White",
														 										 "Black",
														 										 "Age",
														 										 # "College educated",
														 										 # "Income \\textgreater 50k",
														 										 "Homeowner",
														 										 "Urban location",
														 										 "Suburban location",
														 										 "Rural location",
														 										 "Personal exposure"),
														 float = F,
														 # out = "descriptive_tab.tex"
														 out = "TableF1.tex"
)


# blame
mean(a$indiv_blame_bi, na.rm=T) # 0.52
# writeLines(as.character(round(100*mean(a$indiv_blame_bi, na.rm=T))), "indiv_blame_bi.tex",sep = "")


# mechanism check
# writeLines(as.character(round(100*abs(mean(a$spending_bi[a$indiv_blame_bi==1],na.rm=T)-mean(a$spending_bi[a$indiv_blame_bi==0],na.rm=T)))), "blame_diff_bi_treatment.tex",sep = "")
# writeLines(as.character(round(100*abs(mean(a$enforcement_bi[a$indiv_blame_bi==1],na.rm=T)-mean(a$enforcement_bi[a$indiv_blame_bi==0],na.rm=T)))), "blame_diff_bi_enforcement.tex",sep = "")


#---------------#
#### Figures ####
#---------------#

#### Manipulation checks: ####
condition.race.mc.90 <- t.test(a$M1[a$condition_race=="Black"]=="Black",a$M1[a$condition_race=="White"]=="Black",conf.level = 0.9)
condition.race.mc.95 <- t.test(a$M1[a$condition_race=="Black"]=="Black",a$M1[a$condition_race=="White"]=="Black",conf.level = 0.95)
condition.gender.mc.90 <- t.test(a$M2[a$condition_gender=="Female"]=="Female",a$M2[a$condition_gender=="Male"]=="Female",conf.level = 0.9)
condition.gender.mc.95 <- t.test(a$M2[a$condition_gender=="Female"]=="Female",a$M2[a$condition_gender=="Male"]=="Female",conf.level = 0.95)
condition.location.mc.90 <- t.test(a$M3[a$condition_location=="Rural"]=="A rural farm",a$M3[a$condition_location!="Rural"]=="A rural farm",conf.level = 0.9)
condition.location.mc.95 <- t.test(a$M3[a$condition_location=="Rural"]=="A rural farm",a$M3[a$condition_location!="Rural"]=="A rural farm",conf.level = 0.95)
condition.pathway.mc.90 <- t.test(a$M4[a$condition_pathway=="Illegal pills"]=="His/her friend illegally gave him/her OxyContin pain pills at a party.",a$M4[a$condition_pathway!="Illegal pills"]=="His/her friend illegally gave him/her OxyContin pain pills at a party.",conf.level = 0.9)
condition.pathway.mc.95 <- t.test(a$M4[a$condition_pathway=="Illegal pills"]=="His/her friend illegally gave him/her OxyContin pain pills at a party.",a$M4[a$condition_pathway!="Illegal pills"]=="His/her friend illegally gave him/her OxyContin pain pills at a party.",conf.level = 0.95)
condition.insurance.mc.90 <- t.test(a$M5[a$condition_insurance=="Private insurance"]=="Insurance purchased from a private provider",a$M5[a$condition_insurance!="Private insurance"]=="Insurance purchased from a private provider",conf.level = 0.9)
condition.insurance.mc.95 <- t.test(a$M5[a$condition_insurance=="Private insurance"]=="Insurance purchased from a private provider",a$M5[a$condition_insurance!="Private insurance"]=="Insurance purchased from a private provider",conf.level = 0.95)

treatment_tab_mc <- data.frame(
	rbind(
		c((condition.race.mc.95$estimate[1]-condition.race.mc.95$estimate[2]),condition.race.mc.95$conf.int[1],condition.race.mc.95$conf.int[2],condition.race.mc.90$conf.int[1],condition.race.mc.90$conf.int[2]),
		c((condition.gender.mc.95$estimate[1]-condition.gender.mc.95$estimate[2]),condition.gender.mc.95$conf.int[1],condition.gender.mc.95$conf.int[2],condition.gender.mc.90$conf.int[1],condition.gender.mc.90$conf.int[2]),
		c((condition.insurance.mc.95$estimate[1]-condition.insurance.mc.95$estimate[2]),condition.insurance.mc.95$conf.int[1],condition.insurance.mc.95$conf.int[2],condition.insurance.mc.90$conf.int[1],condition.insurance.mc.90$conf.int[2]),
		c((condition.pathway.mc.95$estimate[1]-condition.pathway.mc.95$estimate[2]),condition.pathway.mc.95$conf.int[1],condition.pathway.mc.95$conf.int[2],condition.pathway.mc.90$conf.int[1],condition.pathway.mc.90$conf.int[2]),
		c((condition.location.mc.95$estimate[1]-condition.location.mc.95$estimate[2]),condition.location.mc.95$conf.int[1],condition.location.mc.95$conf.int[2],condition.location.mc.90$conf.int[1],condition.location.mc.90$conf.int[2])
	)
)
names(treatment_tab_mc) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_mc$mc <- factor(c("Race:\nblack","Gender:\nfemale","Insurance:\nprivate","Pathway to addiction:\npills at party","Residential location:\nrural"))

# writeLines(as.character(round(condition.race.mc.95$estimate[1]-condition.race.mc.95$estimate[2],2)*100), "race_mc_effect.tex",sep = "")

(plot_treat_mc <- ggplot(treatment_tab_mc, aes(x=mc)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
								data=treatment_tab_mc,
								width=.0, size=0.75) +
	geom_point(aes(y=treatment_effect),
						 data=treatment_tab_mc,size=3) + 
  geom_text(aes(y=treatment_effect,label=round(treatment_effect,2)),nudge_x = 0.2) + 
	geom_hline(aes(yintercept=0),lty=2) + 
	ylab("Treatment effect on response option") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.005,1.0)) + 
	scale_x_discrete("Manipulation check",
									 limits=c("Race:\nblack","Gender:\nfemale","Insurance:\nprivate","Pathway to addiction:\npills at party","Residential location:\nrural")) + 
	theme(text=element_text(colour=black,
													# family="",
													size=15))
)

# fname = "all_mcs_treatment.pdf"
fname = "FigureD1.pdf"
CairoPDF(fname, width=10, height=5,family = "sans")
print(plot_treat_mc)
dev.off()

## Manipulation checks: levels
mc_race_tab <- a %>%
  janitor::tabyl(condition_race,M1) %>%
  dplyr::select(condition_race, White,Black,Hispanic,Asian) %>%
  pivot_longer(cols = c(White,Black,Hispanic,Asian),names_to = "recall") %>%
  group_by(condition_race) %>%
  mutate(perc = value/sum(value))

(mc_levels_race <- ggplot(mc_race_tab,aes(x=recall)) + 
  geom_bar(aes(y = perc),stat="identity") +
  geom_text(aes(y=perc,label=paste0(100*round(perc,2),"%")),nudge_y = 0.05) + 
  facet_wrap(~condition_race,labeller = as_labeller(function(x)paste0("Race Condition: ",x))) + 
  scale_y_continuous("",labels=scales::percent_format(accuracy = 1)) + 
  scale_x_discrete("Recalled race as:",limits=c("White","Black","Hispanic","Asian")) + 
  theme_bw()
)
# ggsave(plot = mc_levels_race,filename = "mc_levels_race.pdf",height=3,width=4.3)
ggsave(plot = mc_levels_race,filename = "FigureD2a.pdf",height=3,width=4.3)


mc_gender_tab <- a %>%
  janitor::tabyl(condition_gender,M2) %>%
  dplyr::select(condition_gender, Male, Female) %>%
  pivot_longer(cols = c(Male, Female),names_to = "recall") %>%
  group_by(condition_gender) %>%
  mutate(perc = value/sum(value))

(mc_levels_gender <- ggplot(mc_gender_tab,aes(x=recall)) + 
    geom_bar(aes(y = perc),stat="identity") +
    geom_text(aes(y=perc,label=paste0(100*round(perc,2),"%")),nudge_y = 0.05) + 
    facet_wrap(~condition_gender,labeller = as_labeller(function(x)paste0("Gender Condition: ",x))) + 
    scale_y_continuous("",labels=scales::percent_format(accuracy = 1)) + 
    scale_x_discrete("Recalled gender as:",limits=c("Male","Female")) + 
    theme_bw()
)
# ggsave(plot = mc_levels_gender,filename = "mc_levels_gender.pdf",height=3,width=4.3)
ggsave(plot = mc_levels_gender,filename = "FigureD2b.pdf",height=3,width=4.3)


mc_location_tab <- a %>%
  janitor::tabyl(condition_location,M3) %>%
  dplyr::select(condition_location, `A rural farm`, `A quiet suburb`, `An urban downtown center`) %>%
  pivot_longer(cols = c(`A rural farm`, `A quiet suburb`, `An urban downtown center`),names_to = "recall") %>%
  group_by(condition_location) %>%
  mutate(perc = value/sum(value))

(mc_levels_location <- ggplot(mc_location_tab,aes(x=recall)) + 
    geom_bar(aes(y = perc),stat="identity") +
    geom_text(aes(y=perc,label=paste0(100*round(perc,2),"%")),nudge_y = 0.05) + 
    facet_wrap(~condition_location,labeller = as_labeller(function(x)paste0("Location Condition: ",x))) + 
    scale_y_continuous("",labels=scales::percent_format(accuracy = 1)) + 
    scale_x_discrete("Recalled location as:",limits=c("A rural farm", "A quiet suburb", "An urban downtown center"),labels=as_labeller(function(x)str_wrap(x,width=10))) + 
    theme_bw()
)
# ggsave(plot = mc_levels_location,filename = "mc_levels_location.pdf",height=3,width=6)
ggsave(plot = mc_levels_location,filename = "FigureD2c.pdf",height=3,width=6)



### Experimental treatment effects:
## Recipient Race #### 
condition.race.spending.90 <- t.test(a$spending[a$condition_race=="Black"],a$spending[a$condition_race=="White"],conf.level = 0.9)
condition.race.spending.95 <- t.test(a$spending[a$condition_race=="Black"],a$spending[a$condition_race=="White"],conf.level = 0.95)
condition.race.spending_bi.90 <- t.test(a$spending_bi[a$condition_race=="Black"],a$spending_bi[a$condition_race=="White"],conf.level = 0.9)
condition.race.spending_bi.95 <- t.test(a$spending_bi[a$condition_race=="Black"],a$spending_bi[a$condition_race=="White"],conf.level = 0.95)

condition.race.enforcement.90 <- t.test(a$enforcement[a$condition_race=="Black"],a$enforcement[a$condition_race=="White"],conf.level = 0.9)
condition.race.enforcement.95 <- t.test(a$enforcement[a$condition_race=="Black"],a$enforcement[a$condition_race=="White"],conf.level = 0.95)
condition.race.enforcement_bi.90 <- t.test(a$enforcement_bi[a$condition_race=="Black"],a$enforcement_bi[a$condition_race=="White"],conf.level = 0.9)
condition.race.enforcement_bi.95 <- t.test(a$enforcement_bi[a$condition_race=="Black"],a$enforcement_bi[a$condition_race=="White"],conf.level = 0.95)

condition.race.blame.90 <- t.test(a$indiv_blame[a$condition_race=="Black"],a$indiv_blame[a$condition_race=="White"],conf.level = 0.9)
condition.race.blame.95 <- t.test(a$indiv_blame[a$condition_race=="Black"],a$indiv_blame[a$condition_race=="White"],conf.level = 0.95)
condition.race.blame_bi.90 <- t.test(a$indiv_blame_bi[a$condition_race=="Black"],a$indiv_blame_bi[a$condition_race=="White"],conf.level = 0.9)
condition.race.blame_bi.95 <- t.test(a$indiv_blame_bi[a$condition_race=="Black"],a$indiv_blame_bi[a$condition_race=="White"],conf.level = 0.95)


# matching races
# continuous
condition.race.spending.90_white <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1],a$spending[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.spending.95_white <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1],a$spending[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.spending.90_black <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1],a$spending[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.spending.95_black <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1],a$spending[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.spending.90_matching <- t.test(a$spending[a$race_match==1],a$spending[a$race_match==0],conf.level = 0.90)
condition.race.spending.95_matching <- t.test(a$spending[a$race_match==1],a$spending[a$race_match==0],conf.level = 0.95)

condition.race.punitive.90_white <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1],a$enforcement[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.punitive.95_white <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1],a$enforcement[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.punitive.90_black <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1],a$enforcement[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.punitive.95_black <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1],a$enforcement[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.punitive.90_matching <- t.test(a$enforcement[a$race_match==1],a$enforcement[a$race_match==0],conf.level = 0.90)
condition.race.punitive.95_matching <- t.test(a$enforcement[a$race_match==1],a$enforcement[a$race_match==0],conf.level = 0.95)

condition.race.blame.90_white <- t.test(a$indiv_blame[a$condition_race=="Black" & a$white_resp==1],a$indiv_blame[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.blame.95_white <- t.test(a$indiv_blame[a$condition_race=="Black" & a$white_resp==1],a$indiv_blame[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.blame.90_black <- t.test(a$indiv_blame[a$condition_race=="Black" & a$black_resp==1],a$indiv_blame[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.blame.95_black <- t.test(a$indiv_blame[a$condition_race=="Black" & a$black_resp==1],a$indiv_blame[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.blame.90_matching <- t.test(a$indiv_blame[a$race_match==1],a$indiv_blame[a$race_match==0],conf.level = 0.90)
condition.race.blame.95_matching <- t.test(a$indiv_blame[a$race_match==1],a$indiv_blame[a$race_match==0],conf.level = 0.95)


# binary
condition.race.spending_bi.90_white <- t.test(a$spending_bi[a$condition_race=="Black" & a$white_resp==1],a$spending_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.spending_bi.95_white <- t.test(a$spending_bi[a$condition_race=="Black" & a$white_resp==1],a$spending_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.spending_bi.90_black <- t.test(a$spending_bi[a$condition_race=="Black" & a$black_resp==1],a$spending_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.spending_bi.95_black <- t.test(a$spending_bi[a$condition_race=="Black" & a$black_resp==1],a$spending_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.spending_bi.90_matching <- t.test(a$spending_bi[a$race_match==1],a$spending_bi[a$race_match==0],conf.level = 0.90)
condition.race.spending_bi.95_matching <- t.test(a$spending_bi[a$race_match==1],a$spending_bi[a$race_match==0],conf.level = 0.95)

condition.race.punitive_bi.90_white <- t.test(a$enforcement_bi[a$condition_race=="Black" & a$white_resp==1],a$enforcement_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.punitive_bi.95_white <- t.test(a$enforcement_bi[a$condition_race=="Black" & a$white_resp==1],a$enforcement_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.punitive_bi.90_black <- t.test(a$enforcement_bi[a$condition_race=="Black" & a$black_resp==1],a$enforcement_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.punitive_bi.95_black <- t.test(a$enforcement_bi[a$condition_race=="Black" & a$black_resp==1],a$enforcement_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.punitive_bi.90_matching <- t.test(a$enforcement_bi[a$race_match==1],a$enforcement_bi[a$race_match==0],conf.level = 0.90)
condition.race.punitive_bi.95_matching <- t.test(a$enforcement_bi[a$race_match==1],a$enforcement_bi[a$race_match==0],conf.level = 0.95)

condition.race.blame_bi.90_white <- t.test(a$indiv_blame_bi[a$condition_race=="Black" & a$white_resp==1],a$indiv_blame_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.blame_bi.95_white <- t.test(a$indiv_blame_bi[a$condition_race=="Black" & a$white_resp==1],a$indiv_blame_bi[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.blame_bi.90_black <- t.test(a$indiv_blame_bi[a$condition_race=="Black" & a$black_resp==1],a$indiv_blame_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.blame_bi.95_black <- t.test(a$indiv_blame_bi[a$condition_race=="Black" & a$black_resp==1],a$indiv_blame_bi[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.blame_bi.90_matching <- t.test(a$indiv_blame_bi[a$race_match==1],a$indiv_blame_bi[a$race_match==0],conf.level = 0.90)
condition.race.blame_bi.95_matching <- t.test(a$indiv_blame_bi[a$race_match==1],a$indiv_blame_bi[a$race_match==0],conf.level = 0.95)

p_vals_figG6 <- c(condition.race.spending_bi.95_black$p.value, condition.race.spending_bi.95_white$p.value,
                   condition.race.punitive_bi.95_black$p.value, condition.race.punitive_bi.95_white$p.value) 

treatment_tab <- data.frame(
	rbind(
		c((condition.race.spending_bi.95_white$estimate[1]-condition.race.spending_bi.95_white$estimate[2]),condition.race.spending_bi.95_white$conf.int[1],condition.race.spending_bi.95_white$conf.int[2],condition.race.spending_bi.90_white$conf.int[1],condition.race.spending_bi.90_white$conf.int[2]),
		c((condition.race.spending_bi.95_black$estimate[1]-condition.race.spending_bi.95_black$estimate[2]),condition.race.spending_bi.95_black$conf.int[1],condition.race.spending_bi.95_black$conf.int[2],condition.race.spending_bi.90_black$conf.int[1],condition.race.spending_bi.90_black$conf.int[2]),
		c((condition.race.spending_bi.95$estimate[1]-condition.race.spending_bi.95$estimate[2]),condition.race.spending_bi.95$conf.int[1],condition.race.spending_bi.95$conf.int[2],condition.race.spending_bi.90$conf.int[1],condition.race.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("White\nRespondents","Black\nRespondents","Full\nSample"))
treatment_tab$dv <- "Treatment Spending\n(binary scale)"

treatment_tab_punitive <- data.frame(
  rbind(
    c((condition.race.punitive_bi.95_white$estimate[1]-condition.race.punitive_bi.95_white$estimate[2]),condition.race.punitive_bi.95_white$conf.int[1],condition.race.punitive_bi.95_white$conf.int[2],condition.race.punitive_bi.90_white$conf.int[1],condition.race.punitive_bi.90_white$conf.int[2]),
    c((condition.race.punitive_bi.95_black$estimate[1]-condition.race.punitive_bi.95_black$estimate[2]),condition.race.punitive_bi.95_black$conf.int[1],condition.race.punitive_bi.95_black$conf.int[2],condition.race.punitive_bi.90_black$conf.int[1],condition.race.punitive_bi.90_black$conf.int[2]),
    c((condition.race.enforcement_bi.95$estimate[1]-condition.race.enforcement_bi.95$estimate[2]),condition.race.enforcement_bi.95$conf.int[1],condition.race.enforcement_bi.95$conf.int[2],condition.race.enforcement_bi.90$conf.int[1],condition.race.enforcement_bi.90$conf.int[2])
  )
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$Subset <- factor(c("White\nRespondents","Black\nRespondents","Full\nSample"))
treatment_tab_punitive$dv <- "Enforcement Spending\n(binary scale)"

treatment_tab_blame <- data.frame(
  rbind(
    c((condition.race.blame_bi.95_white$estimate[1]-condition.race.blame_bi.95_white$estimate[2]),condition.race.blame_bi.95_white$conf.int[1],condition.race.blame_bi.95_white$conf.int[2],condition.race.blame_bi.90_white$conf.int[1],condition.race.blame_bi.90_white$conf.int[2]),
    c((condition.race.blame_bi.95_black$estimate[1]-condition.race.blame_bi.95_black$estimate[2]),condition.race.blame_bi.95_black$conf.int[1],condition.race.blame_bi.95_black$conf.int[2],condition.race.blame_bi.90_black$conf.int[1],condition.race.blame_bi.90_black$conf.int[2]),
    c((condition.race.blame_bi.95$estimate[1]-condition.race.blame_bi.95$estimate[2]),condition.race.blame_bi.95$conf.int[1],condition.race.blame_bi.95$conf.int[2],condition.race.blame_bi.90$conf.int[1],condition.race.blame_bi.90$conf.int[2])
  )
)
names(treatment_tab_blame) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_blame$Subset <- factor(c("White\nRespondents","Black\nRespondents","Full\nSample"))
treatment_tab_blame$dv <- "Individual Blame\n(binary scale)"


variables <- bind_rows(treatment_tab,treatment_tab_punitive)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),ordered = T)

(plot_treat <- ggplot(variables, aes(x=Subset)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
                data=variables,
                width=.0, size=0.35) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
                data=variables,
                width=.0, size=1.5) +
  facet_wrap(facets = "dv",nrow = 1) + 
  geom_point(aes(y=treatment_effect),
             data=variables,size=3) + 
  geom_hline(aes(yintercept=0),lty=2) + 
  # xlab(NULL) +
  ylab("Treatment effect of recipient race\non spending support (black-white)") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.19,0.18)) +
  scale_x_discrete(limits=c("White\nRespondents","Black\nRespondents","Full\nSample")) + 
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15))
)

# fname = "race_2dvs_bi_treatment_resprace.pdf"
fname = "FigureG6.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat)
dev.off()


# output treatment effects based on resp race
## black resp
# treatment, continuous
# writeLines(as.character(100*round(abs(condition.race.spending.95_black$estimate[1]-condition.race.spending.95_black$estimate[2]),2)), "race_treatment_effect_blackresp_perc.tex",sep = "")

# punitive, continuous
# writeLines(as.character(100*round(abs(condition.race.punitive.95_black$estimate[1]-condition.race.punitive.95_black$estimate[2]),2)), "race_treatment_effect_punitive_blackresp_perc.tex",sep = "")
# writeLines(as.character(round(condition.race.punitive.95_black$p.value,3)),"race_treatment_punitive_pval_blackresp.tex",sep = "")

## white resp
# treatment, continuous
# writeLines(as.character(100*round(abs(condition.race.spending.95_white$estimate[1]-condition.race.spending.95_white$estimate[2]),2)), "race_treatment_effect_whiteresp_perc.tex",sep = "")

## modeled outcomes
# treatment
fit_race_intx_resprace <- lm(spending ~ condition_race*white_resp, data=subset(a, white_resp=="1"|black_resp=="1"))
summary(fit_race_intx_resprace)

# writeLines(as.character(abs(round(fit_race_intx_resprace$coefficients["condition_raceWhite:white_resp"],2))), "race_intx_resprace.tex",sep = "")
# writeLines(as.character(100*abs(round(fit_race_intx_resprace$coefficients["condition_raceWhite:white_resp"],2))), "race_intx_resprace_perc.tex",sep = "")
# writeLines(as.character(round(summary(fit_race_intx_resprace)$coefficients["condition_raceWhite:white_resp",4],3)),"race_intx_resprace_pval.tex",sep = "")

# enforcement
fit_race_intx_resprace_punitive <- lm(enforcement ~ condition_race*white_resp,data=subset(a, white_resp=="1"|black_resp=="1"))
summary(fit_race_intx_resprace_punitive) 

# writeLines(as.character(abs(round(fit_race_intx_resprace_punitive$coefficients["condition_raceWhite:white_resp"],2))), "race_pun_intx_resprace.tex",sep = "")
# writeLines(as.character(100*abs(round(fit_race_intx_resprace_punitive$coefficients["condition_raceWhite:white_resp"],2))), "race_pun_intx_resprace_perc.tex",sep = "")
# writeLines(as.character(round(summary(fit_race_intx_resprace_punitive)$coefficients["condition_raceWhite:white_resp",4],3)),"race_pun_intx_resprace_pval.tex",sep = "")

# blame
fit_race_intx_resprace_blame <- lm(indiv_blame ~ condition_race*white_resp,data=subset(a, white_resp=="1"|black_resp=="1"))
summary(fit_race_intx_resprace_blame) #no



## Recipient Location: ####
# continuous
condition.context.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location!="Rural"],conf.level = 0.9)
condition.context.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location!="Rural"],conf.level = 0.95)
condition.subvsurban.spending.90 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.spending.95 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvssub.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Suburban"],conf.level = 0.95)
condition.subvsrural.spending.90 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.spending.95 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Rural"],conf.level = 0.95)

condition.context.spending.90_nonrural <- t.test(a$spending[a$condition_location=="Rural" & a$classification!="rural"],a$spending[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.9)
condition.context.spending.95_nonrural <- t.test(a$spending[a$condition_location=="Rural" & a$classification!="rural"],a$spending[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.95)
condition.context.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.context.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvsurban.spending.90_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.spending.95_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.spending.90_suburban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.spending.95_suburban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.spending.90_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.spending.95_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.spending.90_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.spending.95_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.spending.90_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.spending.95_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.spending.90_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.spending.95_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.spending.90_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.spending.95_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.spending.90_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.spending.95_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.spending.90_sub <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.spending.95_sub <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.spending.90_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.spending.95_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)


condition.ruralvsurban.punitive.90 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_suburban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_suburban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.punitive.90 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.punitive.95 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.95)
condition.subvsurban.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.punitive.90 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.punitive.95 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Rural"],conf.level = 0.95)
condition.subvsrural.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.punitive.90 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.punitive.95 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Suburban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)

condition.context.spending.90_matching <-  t.test(a$spending[a$context_match==1],a$spending[a$context_match==0],conf.level = 0.90)
condition.context.spending.95_matching <-  t.test(a$spending[a$context_match==1],a$spending[a$context_match==0],conf.level = 0.95)

condition.context.punitive.90_matching <-  t.test(a$enforcement[a$context_match==1],a$enforcement[a$context_match==0],conf.level = 0.90)
condition.context.punitive.95_matching <-  t.test(a$enforcement[a$context_match==1],a$enforcement[a$context_match==0],conf.level = 0.95)

condition.ruralvsurban.blame.90 <- t.test(a$indiv_blame[a$condition_location=="Rural"],a$indiv_blame[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.blame.95 <- t.test(a$indiv_blame[a$condition_location=="Rural"],a$indiv_blame[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.blame.90_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.blame.95_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)

condition.ruralvsurban.blame.90_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.blame.95_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)

condition.ruralvsurban.blame.90_suburban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.blame.95_suburban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.blame.90_rural <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.blame.95_rural <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)
condition.context.blame.90_matching <-  t.test(a$indiv_blame[a$context_match==1],a$indiv_blame[a$context_match==0],conf.level = 0.90)
condition.context.blame.95_matching <-  t.test(a$indiv_blame[a$context_match==1],a$indiv_blame[a$context_match==0],conf.level = 0.95)

# binary
condition.context.spending_bi.90 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location!="Rural"],conf.level = 0.9)
condition.context.spending_bi.95 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location!="Rural"],conf.level = 0.95)
condition.subvsurban.spending_bi.90 <- t.test(a$spending_bi[a$condition_location=="Suburban"],a$spending_bi[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.spending_bi.95 <- t.test(a$spending_bi[a$condition_location=="Suburban"],a$spending_bi[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.spending_bi.90 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.spending_bi.95 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvssub.spending_bi.90 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.spending_bi.95 <- t.test(a$spending_bi[a$condition_location=="Rural"],a$spending_bi[a$condition_location=="Suburban"],conf.level = 0.95)
condition.subvsrural.spending_bi.90 <- t.test(a$spending_bi[a$condition_location=="Suburban"],a$spending_bi[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.spending_bi.95 <- t.test(a$spending_bi[a$condition_location=="Suburban"],a$spending_bi[a$condition_location=="Rural"],conf.level = 0.95)

condition.context.spending_bi.90_nonrural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification!="rural"],a$spending_bi[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.9)
condition.context.spending_bi.95_nonrural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification!="rural"],a$spending_bi[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.95)
condition.context.spending_bi.90_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.context.spending_bi.95_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvsurban.spending_bi.90_urban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.spending_bi.95_urban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.spending_bi.90_suburban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.spending_bi.95_suburban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.spending_bi.90_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.spending_bi.95_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.spending_bi.90_sub <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.spending_bi.95_sub <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.spending_bi.90_urban <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.spending_bi.95_urban <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$spending_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.spending_bi.90_rural <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$spending_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.spending_bi.95_rural <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$spending_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.spending_bi.90_sub <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.spending_bi.95_sub <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.spending_bi.90_urban <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.spending_bi.95_urban <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.spending_bi.90_rural <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.spending_bi.95_rural <- t.test(a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.spending_bi.90_sub <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.spending_bi.95_sub <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.spending_bi.90_urban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.spending_bi.95_urban <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="urban"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.spending_bi.90_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.spending_bi.95_rural <- t.test(a$spending_bi[a$condition_location=="Rural" & a$classification=="rural"],a$spending_bi[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvsurban.punitive_bi.90 <- t.test(a$enforcement_bi[a$condition_location=="Rural"],a$enforcement_bi[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.punitive_bi.95 <- t.test(a$enforcement_bi[a$condition_location=="Rural"],a$enforcement_bi[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.punitive_bi.90_urban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.punitive_bi.95_urban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.punitive_bi.90_suburban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.punitive_bi.95_suburban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.punitive_bi.90_rural <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.punitive_bi.95_rural <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.punitive_bi.90 <- t.test(a$enforcement_bi[a$condition_location=="Suburban"],a$enforcement_bi[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.punitive_bi.95 <- t.test(a$enforcement_bi[a$condition_location=="Suburban"],a$enforcement_bi[a$condition_location=="Urban"],conf.level = 0.95)
condition.subvsurban.punitive_bi.90_sub <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.punitive_bi.95_sub <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.punitive_bi.90_urban <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.punitive_bi.95_urban <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.punitive_bi.90_rural <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.punitive_bi.95_rural <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.punitive_bi.90 <- t.test(a$enforcement_bi[a$condition_location=="Suburban"],a$enforcement_bi[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.punitive_bi.95 <- t.test(a$enforcement_bi[a$condition_location=="Suburban"],a$enforcement_bi[a$condition_location=="Rural"],conf.level = 0.95)
condition.subvsrural.punitive_bi.90_sub <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.punitive_bi.95_sub <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.punitive_bi.90_urban <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.punitive_bi.95_urban <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.punitive_bi.90_rural <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.punitive_bi.95_rural <- t.test(a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.punitive_bi.90 <- t.test(a$enforcement_bi[a$condition_location=="Rural"],a$enforcement_bi[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.punitive_bi.95 <- t.test(a$enforcement_bi[a$condition_location=="Rural"],a$enforcement_bi[a$condition_location=="Suburban"],conf.level = 0.95)
condition.ruralvssub.punitive_bi.90_urban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.punitive_bi.95_urban <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.punitive_bi.90_sub <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.punitive_bi.95_sub <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.punitive_bi.90_rural <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.punitive_bi.95_rural <- t.test(a$enforcement_bi[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement_bi[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)

condition.context.spending_bi.90_matching <-  t.test(a$spending_bi[a$context_match==1],a$spending_bi[a$context_match==0],conf.level = 0.90)
condition.context.spending_bi.95_matching <-  t.test(a$spending_bi[a$context_match==1],a$spending_bi[a$context_match==0],conf.level = 0.95)

condition.context.punitive_bi.90_matching <-  t.test(a$enforcement_bi[a$context_match==1],a$enforcement_bi[a$context_match==0],conf.level = 0.90)
condition.context.punitive_bi.95_matching <-  t.test(a$enforcement_bi[a$context_match==1],a$enforcement_bi[a$context_match==0],conf.level = 0.95)

condition.ruralvsurban.blame_bi.90 <- t.test(a$indiv_blame_bi[a$condition_location=="Rural"],a$indiv_blame_bi[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.blame_bi.95 <- t.test(a$indiv_blame_bi[a$condition_location=="Rural"],a$indiv_blame_bi[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.blame_bi.90_urban <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.blame_bi.95_urban <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.blame_bi.90_suburban <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.blame_bi.95_suburban <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.blame_bi.90_rural <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.blame_bi.95_rural <- t.test(a$indiv_blame_bi[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame_bi[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)
condition.context.blame_bi.90_matching <-  t.test(a$indiv_blame_bi[a$context_match==1],a$indiv_blame_bi[a$context_match==0],conf.level = 0.90)
condition.context.blame_bi.95_matching <-  t.test(a$indiv_blame_bi[a$context_match==1],a$indiv_blame_bi[a$context_match==0],conf.level = 0.95)


p_vals_figG8 <- c(condition.subvsurban.spending_bi.95_rural$p.value,
                   condition.ruralvsurban.spending_bi.95_rural$p.value,
                   condition.ruralvssub.spending_bi.95_rural$p.value,
                   condition.subvsurban.spending_bi.95_sub$p.value,
                   condition.ruralvsurban.spending_bi.95_suburban$p.value,
                   condition.ruralvssub.spending_bi.95_sub$p.value,
                   condition.subvsurban.spending_bi.95_urban$p.value,
                   condition.ruralvsurban.spending_bi.95_urban$p.value,
                   condition.ruralvssub.spending_bi.95_urban$p.value,
                   condition.subvsurban.punitive_bi.95_rural$p.value,
                   condition.ruralvsurban.punitive_bi.95_rural$p.value,
                   condition.ruralvssub.punitive_bi.95_rural$p.value,
                   condition.subvsurban.punitive_bi.95_sub$p.value,
                   condition.ruralvsurban.punitive_bi.95_suburban$p.value,
                   condition.ruralvssub.punitive_bi.95_sub$p.value,
                   condition.subvsurban.punitive_bi.95_urban$p.value,
                   condition.ruralvsurban.punitive_bi.95_urban$p.value,
                   condition.ruralvssub.punitive_bi.95_urban$p.value)




treatment_tab <- data.frame(
	rbind(
		c((condition.context.spending_bi.95_nonrural$estimate[1]-condition.context.spending_bi.95_nonrural$estimate[2]),condition.context.spending_bi.95_nonrural$conf.int[1],condition.context.spending_bi.95_nonrural$conf.int[2],condition.context.spending_bi.90_nonrural$conf.int[1],condition.context.spending_bi.90_nonrural$conf.int[2]),
		c((condition.context.spending_bi.95_rural$estimate[1]-condition.context.spending_bi.95_rural$estimate[2]),condition.context.spending_bi.95_rural$conf.int[1],condition.context.spending_bi.95_rural$conf.int[2],condition.context.spending_bi.90_rural$conf.int[1],condition.context.spending_bi.90_rural$conf.int[2]),
		c((condition.context.spending_bi.95$estimate[1]-condition.context.spending_bi.95$estimate[2]),condition.context.spending_bi.95$conf.int[1],condition.context.spending_bi.95$conf.int[2],condition.context.spending_bi.90$conf.int[1],condition.context.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Non-Rural\nRespondents","Rural\nRespondents","Full\nSample"))




treatment_tab <- data.frame(
	rbind(
		c((condition.ruralvsurban.spending_bi.95_urban$estimate[1]-condition.ruralvsurban.spending_bi.95_urban$estimate[2]),condition.ruralvsurban.spending_bi.95_urban$conf.int[1],condition.ruralvsurban.spending_bi.95_urban$conf.int[2],condition.ruralvsurban.spending_bi.90_urban$conf.int[1],condition.ruralvsurban.spending_bi.90_urban$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95_suburban$estimate[1]-condition.ruralvsurban.spending_bi.95_suburban$estimate[2]),condition.ruralvsurban.spending_bi.95_suburban$conf.int[1],condition.ruralvsurban.spending_bi.95_suburban$conf.int[2],condition.ruralvsurban.spending_bi.90_suburban$conf.int[1],condition.ruralvsurban.spending_bi.90_suburban$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95_rural$estimate[1]-condition.ruralvsurban.spending_bi.95_rural$estimate[2]),condition.ruralvsurban.spending_bi.95_rural$conf.int[1],condition.ruralvsurban.spending_bi.95_rural$conf.int[2],condition.ruralvsurban.spending_bi.90_rural$conf.int[1],condition.ruralvsurban.spending_bi.90_rural$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95$estimate[1]-condition.ruralvsurban.spending_bi.95$estimate[2]),condition.ruralvsurban.spending_bi.95$conf.int[1],condition.ruralvsurban.spending_bi.95$conf.int[2],condition.ruralvsurban.spending_bi.90$conf.int[1],condition.ruralvsurban.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab$dv <- "Treatment Spending"




# respondent context with 3 DVs
treatment_tab_punitive <- data.frame(
  rbind(
    c((condition.ruralvsurban.punitive_bi.95_urban$estimate[1]-condition.ruralvsurban.punitive_bi.95_urban$estimate[2]),condition.ruralvsurban.punitive_bi.95_urban$conf.int[1],condition.ruralvsurban.punitive_bi.95_urban$conf.int[2],condition.ruralvsurban.punitive_bi.90_urban$conf.int[1],condition.ruralvsurban.punitive_bi.90_urban$conf.int[2]),
    c((condition.ruralvsurban.punitive_bi.95_suburban$estimate[1]-condition.ruralvsurban.punitive_bi.95_suburban$estimate[2]),condition.ruralvsurban.punitive_bi.95_suburban$conf.int[1],condition.ruralvsurban.punitive_bi.95_suburban$conf.int[2],condition.ruralvsurban.punitive_bi.90_suburban$conf.int[1],condition.ruralvsurban.punitive_bi.90_suburban$conf.int[2]),
    c((condition.ruralvsurban.punitive_bi.95_rural$estimate[1]-condition.ruralvsurban.punitive_bi.95_rural$estimate[2]),condition.ruralvsurban.punitive_bi.95_rural$conf.int[1],condition.ruralvsurban.punitive_bi.95_rural$conf.int[2],condition.ruralvsurban.punitive_bi.90_rural$conf.int[1],condition.ruralvsurban.punitive_bi.90_rural$conf.int[2]),
    c((condition.ruralvsurban.punitive_bi.95$estimate[1]-condition.ruralvsurban.punitive_bi.95$estimate[2]),condition.ruralvsurban.punitive_bi.95$conf.int[1],condition.ruralvsurban.punitive_bi.95$conf.int[2],condition.ruralvsurban.punitive_bi.90$conf.int[1],condition.ruralvsurban.punitive_bi.90$conf.int[2])
  )
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive$dv <- "Enforcement Spending"

treatment_tab_blame <- data.frame(
  rbind(
    c((condition.ruralvsurban.blame_bi.95_urban$estimate[1]-condition.ruralvsurban.blame_bi.95_urban$estimate[2]),condition.ruralvsurban.blame_bi.95_urban$conf.int[1],condition.ruralvsurban.blame_bi.95_urban$conf.int[2],condition.ruralvsurban.blame_bi.90_urban$conf.int[1],condition.ruralvsurban.blame_bi.90_urban$conf.int[2]),
    c((condition.ruralvsurban.blame_bi.95_suburban$estimate[1]-condition.ruralvsurban.blame_bi.95_suburban$estimate[2]),condition.ruralvsurban.blame_bi.95_suburban$conf.int[1],condition.ruralvsurban.blame_bi.95_suburban$conf.int[2],condition.ruralvsurban.blame_bi.90_suburban$conf.int[1],condition.ruralvsurban.blame_bi.90_suburban$conf.int[2]),
    c((condition.ruralvsurban.blame_bi.95_rural$estimate[1]-condition.ruralvsurban.blame_bi.95_rural$estimate[2]),condition.ruralvsurban.blame_bi.95_rural$conf.int[1],condition.ruralvsurban.blame_bi.95_rural$conf.int[2],condition.ruralvsurban.blame_bi.90_rural$conf.int[1],condition.ruralvsurban.blame_bi.90_rural$conf.int[2]),
    c((condition.ruralvsurban.blame_bi.95$estimate[1]-condition.ruralvsurban.blame_bi.95$estimate[2]),condition.ruralvsurban.blame_bi.95$conf.int[1],condition.ruralvsurban.blame_bi.95$conf.int[2],condition.ruralvsurban.blame_bi.90$conf.int[1],condition.ruralvsurban.blame_bi.90$conf.int[2])
  )
)
names(treatment_tab_blame) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_blame$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_blame$dv <- "Individual Blame"



### Both outcomes, race match DVs:
variables <- bind_rows(treatment_tab,treatment_tab_punitive)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending","Enforcement Spending"),ordered = T)




treatment_tab <- data.frame(
	rbind(
		c((condition.ruralvsurban.spending_bi.95_urban$estimate[1]-condition.ruralvsurban.spending_bi.95_urban$estimate[2]),condition.ruralvsurban.spending_bi.95_urban$conf.int[1],condition.ruralvsurban.spending_bi.95_urban$conf.int[2],condition.ruralvsurban.spending_bi.90_urban$conf.int[1],condition.ruralvsurban.spending_bi.90_urban$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95_suburban$estimate[1]-condition.ruralvsurban.spending_bi.95_suburban$estimate[2]),condition.ruralvsurban.spending_bi.95_suburban$conf.int[1],condition.ruralvsurban.spending_bi.95_suburban$conf.int[2],condition.ruralvsurban.spending_bi.90_suburban$conf.int[1],condition.ruralvsurban.spending_bi.90_suburban$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95_rural$estimate[1]-condition.ruralvsurban.spending_bi.95_rural$estimate[2]),condition.ruralvsurban.spending_bi.95_rural$conf.int[1],condition.ruralvsurban.spending_bi.95_rural$conf.int[2],condition.ruralvsurban.spending_bi.90_rural$conf.int[1],condition.ruralvsurban.spending_bi.90_rural$conf.int[2]),
		c((condition.ruralvsurban.spending_bi.95$estimate[1]-condition.ruralvsurban.spending_bi.95$estimate[2]),condition.ruralvsurban.spending_bi.95$conf.int[1],condition.ruralvsurban.spending_bi.95$conf.int[2],condition.ruralvsurban.spending_bi.90$conf.int[1],condition.ruralvsurban.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$comparison <- "Rural - urban"
treatment_tab$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab$dv <- "Treatment Spending\n(binary scale)"

treatment_tab_punitive <- data.frame(
	rbind(
		c((condition.ruralvsurban.punitive_bi.95_urban$estimate[1]-condition.ruralvsurban.punitive_bi.95_urban$estimate[2]),condition.ruralvsurban.punitive_bi.95_urban$conf.int[1],condition.ruralvsurban.punitive_bi.95_urban$conf.int[2],condition.ruralvsurban.punitive_bi.90_urban$conf.int[1],condition.ruralvsurban.punitive_bi.90_urban$conf.int[2]),
		c((condition.ruralvsurban.punitive_bi.95_suburban$estimate[1]-condition.ruralvsurban.punitive_bi.95_suburban$estimate[2]),condition.ruralvsurban.punitive_bi.95_suburban$conf.int[1],condition.ruralvsurban.punitive_bi.95_suburban$conf.int[2],condition.ruralvsurban.punitive_bi.90_suburban$conf.int[1],condition.ruralvsurban.punitive_bi.90_suburban$conf.int[2]),
		c((condition.ruralvsurban.punitive_bi.95_rural$estimate[1]-condition.ruralvsurban.punitive_bi.95_rural$estimate[2]),condition.ruralvsurban.punitive_bi.95_rural$conf.int[1],condition.ruralvsurban.punitive_bi.95_rural$conf.int[2],condition.ruralvsurban.punitive_bi.90_rural$conf.int[1],condition.ruralvsurban.punitive_bi.90_rural$conf.int[2]),
		c((condition.ruralvsurban.punitive_bi.95$estimate[1]-condition.ruralvsurban.punitive_bi.95$estimate[2]),condition.ruralvsurban.punitive_bi.95$conf.int[1],condition.ruralvsurban.punitive_bi.95$conf.int[2],condition.ruralvsurban.punitive_bi.90$conf.int[1],condition.ruralvsurban.punitive_bi.90$conf.int[2])
	)
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$comparison <- "Rural - urban"
treatment_tab_punitive$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive$dv <- "Enforcement Spending\n(binary scale)"

treatment_tab_subvsurb <- data.frame(
	rbind(
		c((condition.subvsurban.spending_bi.95_urban$estimate[1]-condition.subvsurban.spending_bi.95_urban$estimate[2]),condition.subvsurban.spending_bi.95_urban$conf.int[1],condition.subvsurban.spending_bi.95_urban$conf.int[2],condition.subvsurban.spending_bi.90_urban$conf.int[1],condition.subvsurban.spending_bi.90_urban$conf.int[2]),
		c((condition.subvsurban.spending_bi.95_sub$estimate[1]-condition.subvsurban.spending_bi.95_sub$estimate[2]),condition.subvsurban.spending_bi.95_sub$conf.int[1],condition.subvsurban.spending_bi.95_sub$conf.int[2],condition.subvsurban.spending_bi.90_sub$conf.int[1],condition.subvsurban.spending_bi.90_sub$conf.int[2]),
		c((condition.subvsurban.spending_bi.95_rural$estimate[1]-condition.subvsurban.spending_bi.95_rural$estimate[2]),condition.subvsurban.spending_bi.95_rural$conf.int[1],condition.subvsurban.spending_bi.95_rural$conf.int[2],condition.subvsurban.spending_bi.90_rural$conf.int[1],condition.subvsurban.spending_bi.90_rural$conf.int[2]),
		c((condition.subvsurban.spending_bi.95$estimate[1]-condition.subvsurban.spending_bi.95$estimate[2]),condition.subvsurban.spending_bi.95$conf.int[1],condition.subvsurban.spending_bi.95$conf.int[2],condition.subvsurban.spending_bi.90$conf.int[1],condition.subvsurban.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab_subvsurb) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_subvsurb$comparison <- "Suburban - urban"
treatment_tab_subvsurb$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_subvsurb$dv <- "Treatment Spending\n(binary scale)"

treatment_tab_punitive_subvsurb <- data.frame(
	rbind(
		c((condition.subvsurban.punitive_bi.95_urban$estimate[1]-condition.subvsurban.punitive_bi.95_urban$estimate[2]),condition.subvsurban.punitive_bi.95_urban$conf.int[1],condition.subvsurban.punitive_bi.95_urban$conf.int[2],condition.subvsurban.punitive_bi.90_urban$conf.int[1],condition.subvsurban.punitive_bi.90_urban$conf.int[2]),
		c((condition.subvsurban.punitive_bi.95_sub$estimate[1]-condition.subvsurban.punitive_bi.95_sub$estimate[2]),condition.subvsurban.punitive_bi.95_sub$conf.int[1],condition.subvsurban.punitive_bi.95_sub$conf.int[2],condition.subvsurban.punitive_bi.90_sub$conf.int[1],condition.subvsurban.punitive_bi.90_sub$conf.int[2]),
		c((condition.subvsurban.punitive_bi.95_rural$estimate[1]-condition.subvsurban.punitive_bi.95_rural$estimate[2]),condition.subvsurban.punitive_bi.95_rural$conf.int[1],condition.subvsurban.punitive_bi.95_rural$conf.int[2],condition.subvsurban.punitive_bi.90_rural$conf.int[1],condition.subvsurban.punitive_bi.90_rural$conf.int[2]),
		c((condition.subvsurban.punitive_bi.95$estimate[1]-condition.subvsurban.punitive_bi.95$estimate[2]),condition.subvsurban.punitive_bi.95$conf.int[1],condition.subvsurban.punitive_bi.95$conf.int[2],condition.subvsurban.punitive_bi.90$conf.int[1],condition.subvsurban.punitive_bi.90$conf.int[2])
	)
)
names(treatment_tab_punitive_subvsurb) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_subvsurb$comparison <- "Suburban - urban"
treatment_tab_punitive_subvsurb$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_subvsurb$dv <- "Enforcement Spending\n(binary scale)"

treatment_tab_subvsrural <- data.frame(
	rbind(
		c((condition.subvsrural.spending_bi.95_urban$estimate[1]-condition.subvsrural.spending_bi.95_urban$estimate[2]),condition.subvsrural.spending_bi.95_urban$conf.int[1],condition.subvsrural.spending_bi.95_urban$conf.int[2],condition.subvsrural.spending_bi.90_urban$conf.int[1],condition.subvsrural.spending_bi.90_urban$conf.int[2]),
		c((condition.subvsrural.spending_bi.95_sub$estimate[1]-condition.subvsrural.spending_bi.95_sub$estimate[2]),condition.subvsrural.spending_bi.95_sub$conf.int[1],condition.subvsrural.spending_bi.95_sub$conf.int[2],condition.subvsrural.spending_bi.90_sub$conf.int[1],condition.subvsrural.spending_bi.90_sub$conf.int[2]),
		c((condition.subvsrural.spending_bi.95_rural$estimate[1]-condition.subvsrural.spending_bi.95_rural$estimate[2]),condition.subvsrural.spending_bi.95_rural$conf.int[1],condition.subvsrural.spending_bi.95_rural$conf.int[2],condition.subvsrural.spending_bi.90_rural$conf.int[1],condition.subvsrural.spending_bi.90_rural$conf.int[2]),
		c((condition.subvsrural.spending_bi.95$estimate[1]-condition.subvsrural.spending_bi.95$estimate[2]),condition.subvsrural.spending_bi.95$conf.int[1],condition.subvsrural.spending_bi.95$conf.int[2],condition.subvsrural.spending_bi.90$conf.int[1],condition.subvsrural.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab_subvsrural) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_subvsrural$comparison <- "Suburban - rural"
treatment_tab_subvsrural$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_subvsrural$dv <- "Treatment Spending\n(binary scale)"

treatment_tab_punitive_subvsrural <- data.frame(
	rbind(
		c((condition.subvsrural.punitive_bi.95_urban$estimate[1]-condition.subvsrural.punitive_bi.95_urban$estimate[2]),condition.subvsrural.punitive_bi.95_urban$conf.int[1],condition.subvsrural.punitive_bi.95_urban$conf.int[2],condition.subvsrural.punitive_bi.90_urban$conf.int[1],condition.subvsrural.punitive_bi.90_urban$conf.int[2]),
		c((condition.subvsrural.punitive_bi.95_sub$estimate[1]-condition.subvsrural.punitive_bi.95_sub$estimate[2]),condition.subvsrural.punitive_bi.95_sub$conf.int[1],condition.subvsrural.punitive_bi.95_sub$conf.int[2],condition.subvsrural.punitive_bi.90_sub$conf.int[1],condition.subvsrural.punitive_bi.90_sub$conf.int[2]),
		c((condition.subvsrural.punitive_bi.95_rural$estimate[1]-condition.subvsrural.punitive_bi.95_rural$estimate[2]),condition.subvsrural.punitive_bi.95_rural$conf.int[1],condition.subvsrural.punitive_bi.95_rural$conf.int[2],condition.subvsrural.punitive_bi.90_rural$conf.int[1],condition.subvsrural.punitive_bi.90_rural$conf.int[2]),
		c((condition.subvsrural.punitive_bi.95$estimate[1]-condition.subvsrural.punitive_bi.95$estimate[2]),condition.subvsrural.punitive_bi.95$conf.int[1],condition.subvsrural.punitive_bi.95$conf.int[2],condition.subvsrural.punitive_bi.90$conf.int[1],condition.subvsrural.punitive_bi.90$conf.int[2])
	)
)
names(treatment_tab_punitive_subvsrural) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_subvsrural$comparison <- "Suburban - rural"
treatment_tab_punitive_subvsrural$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_subvsrural$dv <- "Enforcement Spending\n(binary scale)"

treatment_tab_ruralvssub <- data.frame(
	rbind(
		c((condition.ruralvssub.spending_bi.95_urban$estimate[1]-condition.ruralvssub.spending_bi.95_urban$estimate[2]),condition.ruralvssub.spending_bi.95_urban$conf.int[1],condition.ruralvssub.spending_bi.95_urban$conf.int[2],condition.ruralvssub.spending_bi.90_urban$conf.int[1],condition.ruralvssub.spending_bi.90_urban$conf.int[2]),
		c((condition.ruralvssub.spending_bi.95_sub$estimate[1]-condition.ruralvssub.spending_bi.95_sub$estimate[2]),condition.ruralvssub.spending_bi.95_sub$conf.int[1],condition.ruralvssub.spending_bi.95_sub$conf.int[2],condition.ruralvssub.spending_bi.90_sub$conf.int[1],condition.ruralvssub.spending_bi.90_sub$conf.int[2]),
		c((condition.ruralvssub.spending_bi.95_rural$estimate[1]-condition.ruralvssub.spending_bi.95_rural$estimate[2]),condition.ruralvssub.spending_bi.95_rural$conf.int[1],condition.ruralvssub.spending_bi.95_rural$conf.int[2],condition.ruralvssub.spending_bi.90_rural$conf.int[1],condition.ruralvssub.spending_bi.90_rural$conf.int[2]),
		c((condition.ruralvssub.spending_bi.95$estimate[1]-condition.ruralvssub.spending_bi.95$estimate[2]),condition.ruralvssub.spending_bi.95$conf.int[1],condition.ruralvssub.spending_bi.95$conf.int[2],condition.ruralvssub.spending_bi.90$conf.int[1],condition.ruralvssub.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab_ruralvssub) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_ruralvssub$comparison <- "Rural - suburban"
treatment_tab_ruralvssub$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_ruralvssub$dv <- "Treatment Spending\n(binary scale)"

treatment_tab_punitive_ruralvssub <- data.frame(
	rbind(
		c((condition.ruralvssub.punitive_bi.95_urban$estimate[1]-condition.ruralvssub.punitive_bi.95_urban$estimate[2]),condition.ruralvssub.punitive_bi.95_urban$conf.int[1],condition.ruralvssub.punitive_bi.95_urban$conf.int[2],condition.ruralvssub.punitive_bi.90_urban$conf.int[1],condition.ruralvssub.punitive_bi.90_urban$conf.int[2]),
		c((condition.ruralvssub.punitive_bi.95_sub$estimate[1]-condition.ruralvssub.punitive_bi.95_sub$estimate[2]),condition.ruralvssub.punitive_bi.95_sub$conf.int[1],condition.ruralvssub.punitive_bi.95_sub$conf.int[2],condition.ruralvssub.punitive_bi.90_sub$conf.int[1],condition.ruralvssub.punitive_bi.90_sub$conf.int[2]),
		c((condition.ruralvssub.punitive_bi.95_rural$estimate[1]-condition.ruralvssub.punitive_bi.95_rural$estimate[2]),condition.ruralvssub.punitive_bi.95_rural$conf.int[1],condition.ruralvssub.punitive_bi.95_rural$conf.int[2],condition.ruralvssub.punitive_bi.90_rural$conf.int[1],condition.ruralvssub.punitive_bi.90_rural$conf.int[2]),
		c((condition.ruralvssub.punitive_bi.95$estimate[1]-condition.ruralvssub.punitive_bi.95$estimate[2]),condition.ruralvssub.punitive_bi.95$conf.int[1],condition.ruralvssub.punitive_bi.95$conf.int[2],condition.ruralvssub.punitive_bi.90$conf.int[1],condition.ruralvssub.punitive_bi.90$conf.int[2])
	)
)
names(treatment_tab_punitive_ruralvssub) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_ruralvssub$comparison <- "Rural - suburban"
treatment_tab_punitive_ruralvssub$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_ruralvssub$dv <- "Enforcement Spending\n(binary scale)"

variables <- bind_rows(treatment_tab,treatment_tab_punitive,treatment_tab_subvsurb,treatment_tab_punitive_subvsurb,treatment_tab_ruralvssub,treatment_tab_punitive_ruralvssub)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),ordered = T)

(plot_treat <- ggplot(variables, aes(x=Subset,group=comparison)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),position=position_dodge(width=0.5),
								data=variables,
								width=.0, size=0.75) +
	geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),position=position_dodge(width=0.5),
								data=variables,
								width=.0, size=1.5) +
	facet_wrap(facets = "dv",nrow = 1) + 
	geom_point(aes(y=treatment_effect,shape=comparison),position=position_dodge(width=0.5),
						 data=variables,size=3) + 
	geom_hline(aes(yintercept=0),lty=2) + 
	# xlab(NULL) +
	ylab("Treatment effect of recipient context\non spending support") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.18,0.15)) +
	scale_x_discrete(limits=c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample")) + 
	scale_shape_discrete("") + 
	coord_flip() +
	theme(text=element_text(colour=black,
													# family="CM Roman",
													size=15),
				legend.position = "bottom")
)

# fname = "context_2dvs_bi_treatment_resploc_3comparisons.pdf"
fname = "FigureG8.pdf"
CairoPDF(fname, width=7, height=5.5)
print(plot_treat)
dev.off()


# output treatment effects based on resp location
fit_context_intx_resploc <- lm(spending ~ (condition_location)*(classification),data=a)



## Recipient gender ####
# cv
condition.gender.spending.90 <- t.test(a$spending[a$condition_gender=="Male"],a$spending[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.spending.95 <- t.test(a$spending[a$condition_gender=="Male"],a$spending[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.spending.90_female <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1],a$spending[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.spending.95_female <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1],a$spending[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.spending.90_male <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0],a$spending[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.spending.95_male <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0],a$spending[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)

condition.gender.spending.90_matching <-  t.test(a$spending[a$gender_match==1],a$spending[a$gender_match==0],conf.level = 0.90)
condition.gender.spending.95_matching <-  t.test(a$spending[a$gender_match==1],a$spending[a$gender_match==0],conf.level = 0.95)

condition.gender.punitive.90 <- t.test(a$enforcement[a$condition_gender=="Male"],a$enforcement[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.punitive.95 <- t.test(a$enforcement[a$condition_gender=="Male"],a$enforcement[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.punitive.90_female <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.punitive.95_female <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.punitive.90_male <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.punitive.95_male <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)
condition.gender.punitive.90_matching <-  t.test(a$enforcement[a$gender_match==1],a$enforcement[a$gender_match==0],conf.level = 0.90)
condition.gender.punitive.95_matching <-  t.test(a$enforcement[a$gender_match==1],a$enforcement[a$gender_match==0],conf.level = 0.95)

condition.gender.blame.90 <- t.test(a$indiv_blame[a$condition_gender=="Male"],a$indiv_blame[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.blame.95 <- t.test(a$indiv_blame[a$condition_gender=="Male"],a$indiv_blame[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.blame.90_female <- t.test(a$indiv_blame[a$condition_gender=="Male" & a$female_resp==1],a$indiv_blame[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.blame.95_female <- t.test(a$indiv_blame[a$condition_gender=="Male" & a$female_resp==1],a$indiv_blame[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.blame.90_male <- t.test(a$indiv_blame[a$condition_gender=="Male" & a$female_resp==0],a$indiv_blame[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.blame.95_male <- t.test(a$indiv_blame[a$condition_gender=="Male" & a$female_resp==0],a$indiv_blame[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)
condition.gender.blame.90_matching <-  t.test(a$indiv_blame[a$gender_match==1],a$indiv_blame[a$gender_match==0],conf.level = 0.90)
condition.gender.blame.95_matching <-  t.test(a$indiv_blame[a$gender_match==1],a$indiv_blame[a$gender_match==0],conf.level = 0.95)

treatment_tab <- data.frame(
  rbind(
    c((condition.gender.spending.95_female$estimate[1]-condition.gender.spending.95_female$estimate[2]),condition.gender.spending.95_female$conf.int[1],condition.gender.spending.95_female$conf.int[2],condition.gender.spending.90_female$conf.int[1],condition.gender.spending.90_female$conf.int[2]),
    c((condition.gender.spending.95_male$estimate[1]-condition.gender.spending.95_male$estimate[2]),condition.gender.spending.95_male$conf.int[1],condition.gender.spending.95_male$conf.int[2],condition.gender.spending.90_male$conf.int[1],condition.gender.spending.90_male$conf.int[2]),
    c((condition.gender.spending.95$estimate[1]-condition.gender.spending.95$estimate[2]),condition.gender.spending.95$conf.int[1],condition.gender.spending.95$conf.int[2],condition.gender.spending.90$conf.int[1],condition.gender.spending.90$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Female\nRespondents","Male\nRespondents","Full\nSample"))
# binary
condition.gender.spending_bi.90 <- t.test(a$spending_bi[a$condition_gender=="Male"],a$spending_bi[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.spending_bi.95 <- t.test(a$spending_bi[a$condition_gender=="Male"],a$spending_bi[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.spending_bi.90_female <- t.test(a$spending_bi[a$condition_gender=="Male" & a$female_resp==1],a$spending_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.spending_bi.95_female <- t.test(a$spending_bi[a$condition_gender=="Male" & a$female_resp==1],a$spending_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.spending_bi.90_male <- t.test(a$spending_bi[a$condition_gender=="Male" & a$female_resp==0],a$spending_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.spending_bi.95_male <- t.test(a$spending_bi[a$condition_gender=="Male" & a$female_resp==0],a$spending_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)

condition.gender.spending_bi.90_matching <-  t.test(a$spending_bi[a$gender_match==1],a$spending_bi[a$gender_match==0],conf.level = 0.90)
condition.gender.spending_bi.95_matching <-  t.test(a$spending_bi[a$gender_match==1],a$spending_bi[a$gender_match==0],conf.level = 0.95)

condition.gender.punitive_bi.90 <- t.test(a$enforcement_bi[a$condition_gender=="Male"],a$enforcement_bi[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.punitive_bi.95 <- t.test(a$enforcement_bi[a$condition_gender=="Male"],a$enforcement_bi[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.punitive_bi.90_female <- t.test(a$enforcement_bi[a$condition_gender=="Male" & a$female_resp==1],a$enforcement_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.punitive_bi.95_female <- t.test(a$enforcement_bi[a$condition_gender=="Male" & a$female_resp==1],a$enforcement_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.punitive_bi.90_male <- t.test(a$enforcement_bi[a$condition_gender=="Male" & a$female_resp==0],a$enforcement_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.punitive_bi.95_male <- t.test(a$enforcement_bi[a$condition_gender=="Male" & a$female_resp==0],a$enforcement_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)
condition.gender.punitive_bi.90_matching <-  t.test(a$enforcement_bi[a$gender_match==1],a$enforcement_bi[a$gender_match==0],conf.level = 0.90)
condition.gender.punitive_bi.95_matching <-  t.test(a$enforcement_bi[a$gender_match==1],a$enforcement_bi[a$gender_match==0],conf.level = 0.95)

condition.gender.blame_bi.90 <- t.test(a$indiv_blame_bi[a$condition_gender=="Male"],a$indiv_blame_bi[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.blame_bi.95 <- t.test(a$indiv_blame_bi[a$condition_gender=="Male"],a$indiv_blame_bi[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.blame_bi.90_female <- t.test(a$indiv_blame_bi[a$condition_gender=="Male" & a$female_resp==1],a$indiv_blame_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.blame_bi.95_female <- t.test(a$indiv_blame_bi[a$condition_gender=="Male" & a$female_resp==1],a$indiv_blame_bi[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.blame_bi.90_male <- t.test(a$indiv_blame_bi[a$condition_gender=="Male" & a$female_resp==0],a$indiv_blame_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.blame_bi.95_male <- t.test(a$indiv_blame_bi[a$condition_gender=="Male" & a$female_resp==0],a$indiv_blame_bi[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)
condition.gender.blame_bi.90_matching <-  t.test(a$indiv_blame_bi[a$gender_match==1],a$indiv_blame_bi[a$gender_match==0],conf.level = 0.90)
condition.gender.blame_bi.95_matching <-  t.test(a$indiv_blame_bi[a$gender_match==1],a$indiv_blame_bi[a$gender_match==0],conf.level = 0.95)


p_vals_figG7 <- c(condition.gender.spending_bi.95_male$p.value, condition.gender.spending_bi.95_female$p.value,
                   condition.gender.punitive_bi.95_male$p.value, condition.gender.punitive_bi.95_female$p.value)


p_vals_figG9 <- c(condition.race.spending_bi.95_matching$p.value,
                   condition.gender.spending_bi.95_matching$p.value,
                   condition.context.spending_bi.95_matching$p.value,
                   condition.race.punitive_bi.95_matching$p.value,
                   condition.gender.punitive_bi.95_matching$p.value,
                   condition.context.punitive_bi.95_matching$p.value)

treatment_tab <- data.frame(
	rbind(
		c((condition.gender.spending_bi.95_female$estimate[1]-condition.gender.spending_bi.95_female$estimate[2]),condition.gender.spending_bi.95_female$conf.int[1],condition.gender.spending_bi.95_female$conf.int[2],condition.gender.spending_bi.90_female$conf.int[1],condition.gender.spending_bi.90_female$conf.int[2]),
		c((condition.gender.spending_bi.95_male$estimate[1]-condition.gender.spending_bi.95_male$estimate[2]),condition.gender.spending_bi.95_male$conf.int[1],condition.gender.spending_bi.95_male$conf.int[2],condition.gender.spending_bi.90_male$conf.int[1],condition.gender.spending_bi.90_male$conf.int[2]),
		c((condition.gender.spending_bi.95$estimate[1]-condition.gender.spending_bi.95$estimate[2]),condition.gender.spending_bi.95$conf.int[1],condition.gender.spending_bi.95$conf.int[2],condition.gender.spending_bi.90$conf.int[1],condition.gender.spending_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Female\nRespondents","Male\nRespondents","Full\nSample"))




## both funding measures:
treatment_tab <- data.frame(
	rbind(
		c((condition.gender.spending_bi.95_female$estimate[1]-condition.gender.spending_bi.95_female$estimate[2]),condition.gender.spending_bi.95_female$conf.int[1],condition.gender.spending_bi.95_female$conf.int[2],condition.gender.spending_bi.90_female$conf.int[1],condition.gender.spending_bi.90_female$conf.int[2]),
		c((condition.gender.spending_bi.95_male$estimate[1]-condition.gender.spending_bi.95_male$estimate[2]),condition.gender.spending_bi.95_male$conf.int[1],condition.gender.spending_bi.95_male$conf.int[2],condition.gender.spending_bi.90_male$conf.int[1],condition.gender.spending_bi.90_male$conf.int[2]),
		c((condition.gender.spending_bi.95$estimate[1]-condition.gender.spending_bi.95$estimate[2]),condition.gender.spending_bi.95$conf.int[1],condition.gender.spending_bi.95$conf.int[2],condition.gender.spending_bi.90$conf.int[1],condition.gender.spending_bi.90$conf.int[2]),
		
		c((condition.gender.punitive_bi.95_female$estimate[1]-condition.gender.punitive_bi.95_female$estimate[2]),condition.gender.punitive_bi.95_female$conf.int[1],condition.gender.punitive_bi.95_female$conf.int[2],condition.gender.punitive_bi.90_female$conf.int[1],condition.gender.punitive_bi.90_female$conf.int[2]),
		c((condition.gender.punitive_bi.95_male$estimate[1]-condition.gender.punitive_bi.95_male$estimate[2]),condition.gender.punitive_bi.95_male$conf.int[1],condition.gender.punitive_bi.95_male$conf.int[2],condition.gender.punitive_bi.90_male$conf.int[1],condition.gender.punitive_bi.90_male$conf.int[2]),
		c((condition.gender.punitive_bi.95$estimate[1]-condition.gender.punitive_bi.95$estimate[2]),condition.gender.punitive_bi.95$conf.int[1],condition.gender.punitive_bi.95$conf.int[2],condition.gender.punitive_bi.90$conf.int[1],condition.gender.punitive_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Women\nRespondents","Men\nRespondents","Full\nSample"))
treatment_tab$outcome <- factor(rep(c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),each=3),
																levels = c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),
																ordered = T)



(plot_treat <- ggplot(treatment_tab, aes(x=Subset)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
								data=treatment_tab,
								width=.0, size=0.75) +
	geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
								data=treatment_tab,
								width=.0, size=1.5) +
	geom_point(aes(y=treatment_effect),
						 data=treatment_tab,size=3) + 
	facet_wrap(~outcome, nrow=1) + 
	geom_hline(aes(yintercept=0),lty=2) + 
	# xlab(NULL) +
	ylab("Treatment effect of recipient gender\non funding support (male - female)") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.125,0.08)) +
	scale_x_discrete(limits=c("Women\nRespondents","Men\nRespondents","Full\nSample")) + 
	coord_flip() +
	theme(text=element_text(colour=black,
													# family="CM Roman",
													size=15))
)

# fname = "gender_both_bi_treatment_respgender.pdf"
fname = "FigureG7.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat)
dev.off()





treatment_tab <- data.frame(
	rbind(
		c((condition.gender.blame_bi.95_female$estimate[1]-condition.gender.blame_bi.95_female$estimate[2]),condition.gender.blame_bi.95_female$conf.int[1],condition.gender.blame_bi.95_female$conf.int[2],condition.gender.blame_bi.90_female$conf.int[1],condition.gender.blame_bi.90_female$conf.int[2]),
		c((condition.gender.blame_bi.95_male$estimate[1]-condition.gender.blame_bi.95_male$estimate[2]),condition.gender.blame_bi.95_male$conf.int[1],condition.gender.blame_bi.95_male$conf.int[2],condition.gender.blame_bi.90_male$conf.int[1],condition.gender.blame_bi.90_male$conf.int[2]),
		c((condition.gender.blame_bi.95$estimate[1]-condition.gender.blame_bi.95$estimate[2]),condition.gender.blame_bi.95$conf.int[1],condition.gender.blame_bi.95$conf.int[2],condition.gender.blame_bi.90$conf.int[1],condition.gender.blame_bi.90$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Female\nRespondents","Male\nRespondents","Full\nSample"))



fit_gender_intx_respgender <- lm(spending ~ (condition_gender=="Female")*(female_resp),data=a)

fit_gender_intx_respgender <- lm(enforcement ~ (condition_gender=="Female")*(female_resp),data=a)


##### Identity matching, binary outcome ------------------------------------------
treatment_tab <- data.frame(
  rbind(
    c((condition.race.spending_bi.95_matching$estimate[1]-condition.race.spending_bi.95_matching$estimate[2]),condition.race.spending_bi.95_matching$conf.int[1],condition.race.spending_bi.95_matching$conf.int[2],condition.race.spending_bi.90_matching$conf.int[1],condition.race.spending_bi.90_matching$conf.int[2]),
    c((condition.gender.spending_bi.95_matching$estimate[1]-condition.gender.spending_bi.95_matching$estimate[2]),condition.gender.spending_bi.95_matching$conf.int[1],condition.gender.spending_bi.95_matching$conf.int[2],condition.gender.spending_bi.90_matching$conf.int[1],condition.gender.spending_bi.90_matching$conf.int[2]),
    c((condition.context.spending_bi.95_matching$estimate[1]-condition.context.spending_bi.95_matching$estimate[2]),condition.context.spending_bi.95_matching$conf.int[1],condition.context.spending_bi.95_matching$conf.int[2],condition.context.spending_bi.90_matching$conf.int[1],condition.context.spending_bi.90_matching$conf.int[2]),
    
    c((condition.race.punitive_bi.95_matching$estimate[1]-condition.race.punitive_bi.95_matching$estimate[2]),condition.race.punitive_bi.95_matching$conf.int[1],condition.race.punitive_bi.95_matching$conf.int[2],condition.race.punitive_bi.90_matching$conf.int[1],condition.race.punitive_bi.90_matching$conf.int[2]),
    c((condition.gender.punitive_bi.95_matching$estimate[1]-condition.gender.punitive_bi.95_matching$estimate[2]),condition.gender.punitive_bi.95_matching$conf.int[1],condition.gender.punitive_bi.95_matching$conf.int[2],condition.gender.punitive_bi.90_matching$conf.int[1],condition.gender.punitive_bi.90_matching$conf.int[2]),
    c((condition.context.punitive_bi.95_matching$estimate[1]-condition.context.punitive_bi.95_matching$estimate[2]),condition.context.punitive_bi.95_matching$conf.int[1],condition.context.punitive_bi.95_matching$conf.int[2],condition.context.punitive_bi.90_matching$conf.int[1],condition.context.punitive_bi.90_matching$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Manipulation <- factor(rep(c("Race","Gender","Residential\nContext"),times=2))
treatment_tab$outcome <- factor(rep(c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),each=3),
                                levels = c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),
                                ordered = T)

(plot_treat_matching_all <- ggplot(treatment_tab,aes(x=Manipulation)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
                data=treatment_tab,
                width=.0, size=0.75) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
                data=treatment_tab,
                width=.0, size=1.5) +
  geom_point(aes(y=treatment_effect),
             data=treatment_tab,
             size=3) +
  geom_hline(aes(yintercept=0),lty=2) + 
  facet_wrap(~ outcome,nrow=1) + 
  # xlab(NULL) +
  ylab("Treatment effect of recipient attribute\non outcome (identity matching - non-matching)") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.05,0.085)) + 
  scale_x_discrete(limits=c("Residential\nContext","Gender","Race")) +
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15))
)

# fname = "identity_2dvs_treatment_matching_bi.pdf"
fname = "FigureG9.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat_matching_all)
dev.off()



#### Conjoint plots for identity on two DVs ####
## Treatment effects for all attributes:
reg <- lm(spending_bi ~ factor(condition_race,levels = c("White","Black")) + 
            factor(condition_gender,levels=c("Male","Female")) + 
            factor(condition_location,levels=c("Urban","Suburban","Rural")), data=a)
summary(reg)
p_vals_figG5a <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural"
                      )

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

variables$scale <- "Dichotomous Indicator for Funding Support"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(binary scale)"
variables_treatment <- variables

## enforcement
reg <- lm(enforcement_bi ~ factor(condition_race,levels = c("White","Black")) + 
            factor(condition_gender,levels=c("Male","Female")) + 
            factor(condition_location,levels=c("Urban","Suburban","Rural")), data=a)
summary(reg)
p_vals_figG5b <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]


variables$scale <- "Dichotomous Indicator for Funding Support"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(binary scale)"
variables_enforcement <- variables

variables <- bind_rows(variables_treatment,variables_enforcement)
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(binary scale)","Enforcement Spending\n(binary scale)"),ordered = T)

(cjoint_plot_binary <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "dv",nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.105, .105),breaks = seq(-.1,.1,0.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 10:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.105, .105),ylim=c(1, 10)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))  
)

# ggsave(cjoint_plot_binary,height=4,width=7,filename = "conjoint_plot_binary_2dvs.pdf")
ggsave(cjoint_plot_binary,height=4,width=7,filename = "FigureG5.pdf")


#### Conjoint plot for identity on two DVs, continuous ####
reg <- lm(spending ~ factor(condition_race,levels = c("White","Black")) + 
						factor(condition_gender,levels=c("Male","Female")) + 
						factor(condition_location,levels=c("Urban","Suburban","Rural")), data=a)
summary(reg)
p_vals_fig2a <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c("Race:","    White","    Black",
											"Gender:","    Man","    Woman",
											"Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables_treatment_cont <- variables

### Enforcement DV:
reg <- lm(enforcement ~ factor(condition_race,levels = c("White","Black")) + 
						factor(condition_gender,levels=c("Male","Female")) + 
						factor(condition_location,levels=c("Urban","Suburban","Rural")), data=a)
summary(reg)
p_vals_fig2b <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c("Race:","    White","    Black",
											"Gender:","    Man","    Woman",
											"Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables_enforcement_cont <- variables

variables <- bind_rows(variables_treatment_cont,variables_enforcement_cont)
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
	geom_errorbarh(data = variables, aes(y = order, 
																			 xmin = coef + qnorm(0.025)*SE, 
																			 xmax = coef + qnorm(0.975)*SE), 
								 height=0,lwd=0.75,colour = 'black') +
	geom_errorbarh(data = variables, aes(y = order, 
																			 xmin = coef + qnorm(0.05)*SE, 
																			 xmax = coef + qnorm(0.95)*SE), 
								 height=0, lwd=1.5, colour = 'black') +
	# geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
	# geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
	geom_point(colour = 'black', size = 2.75) + 
	facet_wrap(facets = "dv",nrow = 1) + 
	scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.055, .055),breaks = c(-.05,0,.05)) +
	scale_y_continuous("Substance User Attribute",breaks = 10:1,labels=attribute_labels) +
	coord_cartesian(xlim=c(-.055, .055),ylim=c(1, 10)) +
	geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
	theme_bw() +
	theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
	theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
	# theme(axis.title.y = element_blank()) +
	theme(axis.text.x = element_text(size=12)) + 
	theme(axis.title.x = element_text(size=12))   
)

## Figure 2:
# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_continuous_2dvs.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "Figure2.pdf")


#### Additional Treatments as 2 DVs ####
reg <- lm(spending ~ 
            factor(condition_insurance,levels=c("Medicaid","ACA Marketplace","Private insurance")) + 
            factor(condition_pathway,levels=c("Heroin","Illegal pills","Prescription pills")), data=a)
summary(reg)
p_vals_figH10a <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c( "Insurance Coverage:","    Medicaid","    ACA Marketplace","    Private",
                       "Initial Opioid Use:","    Heroin","    Illegal OxyContin","    Legal OxyContin")
variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# Insurance coverage
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]
variables[4,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# pathway to addiction
variables[6,c(2,3)] <- 0
variables[7,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[8,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 8:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain", "plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables_treatment_cont <- variables

### Enforcement DV:
reg <- lm(enforcement ~ 
            factor(condition_insurance,levels=c("Medicaid","ACA Marketplace","Private insurance")) + 
            factor(condition_pathway,levels=c("Heroin","Illegal pills","Prescription pills")), data=a)
summary(reg)
p_vals_figH10b <- c(summary(reg)$coefficients[2:5, 4])

attribute_labels <- c( "Insurance Coverage:","    Medicaid","    ACA Marketplace","    Private",
                       "Initial Opioid Use:","    Heroin","    Illegal OxyContin","    Legal OxyContin")
variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# Insurance coverage
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]
variables[4,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# pathway to addiction
variables[6,c(2,3)] <- 0
variables[7,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[8,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 8:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain", "plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables_enforcement_cont <- variables

variables <- bind_rows(variables_treatment_cont,variables_enforcement_cont)
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "dv",nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.1, .1),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 8:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.1, .1), ylim=c(1, 8)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_continuous_2dvs_insurance_pathway.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureH10.pdf")



### Blame DV:
reg <- lm(indiv_blame ~ 
            factor(condition_pathway,levels=c("Heroin","Illegal pills","Prescription pills")), data=a)
summary(reg)
p_vals_fig7 <- c(summary(reg)$coefficients[2:3, 4])

attribute_labels <- c(
                       "Initial Opioid Use:","    Heroin","    Illegal OxyContin","    Legal OxyContin")
variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# pathway to addiction
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]
variables[4,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 4:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain", "plain")

variables$dv <- "Individual Blame\n(interval scale)"

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "dv", nrow = 1) + 
  scale_x_continuous("Difference in perceived blame\nfrom baseline user attribute", limits = c(-.15, .15),breaks = c(-.1,0,.1)) +
  scale_y_continuous("Initial Opioid Use:", breaks = 4:1, labels=attribute_labels,limits=c(0.5,3.5)) +
  coord_cartesian(xlim=c(-.15, .15)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12)) + 
  theme(axis.title.y = element_text(angle=0,size=12,face="bold",hjust=0,margin = margin(0,-7,0,0,unit = "lines")))   
)

# ggsave(cjoint_plot_cont,height=3,width=4,filename = "conjoint_plot_continuous_blame_pathway.pdf")
ggsave(cjoint_plot_cont,height=3,width=4,filename = "Figure7.pdf")


##### Conjoint plot by resp PID -------------
reg_dem <- lm(spending ~ factor(condition_race,levels = c("White","Black")) + 
                factor(condition_gender,levels=c("Male","Female")) + 
                factor(condition_location,levels=c("Urban","Suburban","Rural")), data=filter(a,pid==-1))
summary(reg_dem)
p_vals_figJ19a <- c(summary(reg_dem)$coefficients[2:5, 4])


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA


# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg_dem)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg_dem)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg_dem)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg_dem)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables_treatment_cont <- variables


### Enforcement DV:
reg_dem <- lm(enforcement ~ factor(condition_race,levels = c("White","Black")) + 
                factor(condition_gender,levels=c("Male","Female")) + 
                factor(condition_location,levels=c("Urban","Suburban","Rural")), data=filter(a,pid==-1))
summary(reg_dem)
p_vals_figJ19b <- c(summary(reg_dem)$coefficients[2:5, 4])


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA


# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg_dem)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg_dem)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg_dem)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg_dem)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables_enforcement_cont <- variables

variables <- bind_rows(variables_treatment_cont,variables_enforcement_cont)
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
    geom_errorbarh(data = variables, aes(y = order, 
                                         xmin = coef + qnorm(0.025)*SE, 
                                         xmax = coef + qnorm(0.975)*SE), 
                   height=0,lwd=0.75,colour = 'black') +
    geom_errorbarh(data = variables, aes(y = order, 
                                         xmin = coef + qnorm(0.05)*SE, 
                                         xmax = coef + qnorm(0.95)*SE), 
                   height=0, lwd=1.5, colour = 'black') +
    # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
    # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
    geom_point(colour = 'black', size = 2.75) + 
    facet_wrap(facets = "dv",nrow = 1) + 
    scale_x_continuous("Difference in Outcome from Baseline User Attribute", limits = c(-.095, .095),breaks = c(-.05,0,.05)) +
    scale_y_continuous("Substance User Attribute",breaks = 10:1,labels=attribute_labels) +
    coord_cartesian(xlim=c(-.065, .065),ylim=c(1, 10)) +
    geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
    theme_bw() +
    theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
    theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
    # theme(axis.title.y = element_blank()) +
    theme(axis.text.x = element_text(size=12)) + 
    theme(axis.title.x = element_text(size=12))  
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_continuous_2dvs_dems.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureJ19.pdf")


reg_rep <- lm(spending ~ factor(condition_race,levels = c("White","Black")) + 
                factor(condition_gender,levels=c("Male","Female")) + 
                factor(condition_location,levels=c("Urban","Suburban","Rural")), data=filter(a,pid==1))
summary(reg_rep)
p_vals_figJ20a <- c(summary(reg_rep)$coefficients[2:5, 4])


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA


# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg_rep)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg_rep)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg_rep)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg_rep)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables_treatment_cont <- variables


### Enforcement DV:
reg_rep <- lm(enforcement ~ factor(condition_race,levels = c("White","Black")) + 
                factor(condition_gender,levels=c("Male","Female")) + 
                factor(condition_location,levels=c("Urban","Suburban","Rural")), data=filter(a,pid==1))
summary(reg_rep)
p_vals_figJ20b <- c(summary(reg_rep)$coefficients[2:5, 4])


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Residential location:","    Urban","    Suburban","    Rural")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA


# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg_rep)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg_rep)$coefficients[3, c(1,2)]

# residential context
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg_rep)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg_rep)$coefficients[5, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 10:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables_enforcement_cont <- variables

variables <- bind_rows(variables_treatment_cont,variables_enforcement_cont)
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
    geom_errorbarh(data = variables, aes(y = order, 
                                         xmin = coef + qnorm(0.025)*SE, 
                                         xmax = coef + qnorm(0.975)*SE), 
                   height=0,lwd=0.75,colour = 'black') +
    geom_errorbarh(data = variables, aes(y = order, 
                                         xmin = coef + qnorm(0.05)*SE, 
                                         xmax = coef + qnorm(0.95)*SE), 
                   height=0, lwd=1.5, colour = 'black') +
    # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
    # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
    geom_point(colour = 'black', size = 2.75) + 
    facet_wrap(facets = "dv",nrow = 1) + 
    scale_x_continuous("Difference in Outcome from Baseline User Attribute", limits = c(-.095, .095),breaks = c(-.05,0,.05)) +
    scale_y_continuous("Substance User Attribute",breaks = 10:1,labels=attribute_labels) +
    coord_cartesian(xlim=c(-.065, .065),ylim=c(1, 10)) +
    geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
    theme_bw() +
    theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
    theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
    # theme(axis.title.y = element_blank()) +
    theme(axis.text.x = element_text(size=12)) + 
    theme(axis.title.x = element_text(size=12))  
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_continuous_2dvs_reps.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureJ20.pdf")


##### Heterogeneous by resp race, cont outcome ---------------------------------
condition.race.spending.95 <- t.test(a$spending[a$condition_race=="Black"],a$spending[a$condition_race=="White"],conf.level = 0.95)
condition.race.spending.90 <- t.test(a$spending[a$condition_race=="Black"],a$spending[a$condition_race=="White"],conf.level = 0.9)
condition.race.spending.90_white <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1],a$spending[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.spending.95_white <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1],a$spending[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.spending.90_black <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1],a$spending[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.spending.95_black <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1],a$spending[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.spending.90_matching <- t.test(a$spending[a$race_match==1],a$spending[a$race_match==0],conf.level = 0.90)
condition.race.spending.95_matching <- t.test(a$spending[a$race_match==1],a$spending[a$race_match==0],conf.level = 0.95)

condition.race.punitive.95 <- t.test(a$enforcement[a$condition_race=="Black"],a$enforcement[a$condition_race=="White"],conf.level = 0.95)
condition.race.punitive.90 <- t.test(a$enforcement[a$condition_race=="Black"],a$enforcement[a$condition_race=="White"],conf.level = 0.9)
condition.race.punitive.90_white <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1],a$enforcement[a$condition_race=="White" & a$white_resp==1],conf.level = 0.9)
condition.race.punitive.95_white <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1],a$enforcement[a$condition_race=="White" & a$white_resp==1],conf.level = 0.95)
condition.race.punitive.90_black <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1],a$enforcement[a$condition_race=="White" & a$black_resp==1],conf.level = 0.9)
condition.race.punitive.95_black <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1],a$enforcement[a$condition_race=="White" & a$black_resp==1],conf.level = 0.95)
condition.race.punitive.90_matching <- t.test(a$enforcement[a$race_match==1],a$enforcement[a$race_match==0],conf.level = 0.90)
condition.race.punitive.95_matching <- t.test(a$enforcement[a$race_match==1],a$enforcement[a$race_match==0],conf.level = 0.95)



treatment_tab <- data.frame(
  rbind(
    c((condition.race.spending.95_white$estimate[1]-condition.race.spending.95_white$estimate[2]),condition.race.spending.95_white$conf.int[1],condition.race.spending.95_white$conf.int[2],condition.race.spending.90_white$conf.int[1],condition.race.spending.90_white$conf.int[2]),
    c((condition.race.spending.95_black$estimate[1]-condition.race.spending.95_black$estimate[2]),condition.race.spending.95_black$conf.int[1],condition.race.spending.95_black$conf.int[2],condition.race.spending.90_black$conf.int[1],condition.race.spending.90_black$conf.int[2]),
    c((condition.race.spending.95$estimate[1]-condition.race.spending.95$estimate[2]),condition.race.spending.95$conf.int[1],condition.race.spending.95$conf.int[2],condition.race.spending.90$conf.int[1],condition.race.spending.90$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("White\nRespondents","Black\nRespondents","Full\nSample"))
treatment_tab$dv <- "Treatment Spending\n(interval scale)"


treatment_tab_punitive <- data.frame(
  rbind(
    c((condition.race.punitive.95_white$estimate[1]-condition.race.punitive.95_white$estimate[2]),condition.race.punitive.95_white$conf.int[1],condition.race.punitive.95_white$conf.int[2],condition.race.punitive.90_white$conf.int[1],condition.race.punitive.90_white$conf.int[2]),
    c((condition.race.punitive.95_black$estimate[1]-condition.race.punitive.95_black$estimate[2]),condition.race.punitive.95_black$conf.int[1],condition.race.punitive.95_black$conf.int[2],condition.race.punitive.90_black$conf.int[1],condition.race.punitive.90_black$conf.int[2]),
    c((condition.race.punitive.95$estimate[1]-condition.race.punitive.95$estimate[2]),condition.race.punitive.95$conf.int[1],condition.race.punitive.95$conf.int[2],condition.race.punitive.90$conf.int[1],condition.race.punitive.90$conf.int[2])
  )
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$Subset <- factor(c("White\nRespondents","Black\nRespondents","Full\nSample"))
treatment_tab_punitive$dv <- "Enforcement Spending\n(interval scale)"


variables <- bind_rows(treatment_tab,treatment_tab_punitive)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(plot_treat <- ggplot(variables, aes(x=Subset)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
                data=variables,
                width=.0, size=0.75) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
                data=variables,
                width=.0, size=1.5) +
  facet_wrap(facets = "dv",nrow = 1) + 
  geom_point(aes(y=treatment_effect),
             data=variables,size=3) + 
  geom_hline(aes(yintercept=0),lty=2) + 
  # xlab(NULL) +
  ylab("Treatment effect of substance user race\n(black condition - white condition) on policy support") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.15,0.15)) +
  scale_x_discrete(limits=c("White\nRespondents","Black\nRespondents","Full\nSample")) + 
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15))
)

# fname = "race_2dvs_treatment_resprace_continuous.pdf"
fname = "Figure3.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat)
dev.off()


# p values
p_vals_fig3 <- c(condition.race.spending.95_black$p.value,
                 condition.race.spending.95_white$p.value, 
                   condition.race.punitive.95_black$p.value,
                 condition.race.punitive.95_white$p.value)




##### Heterogeneous by resp location, cont outcome -----------------------------

condition.context.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location!="Rural"],conf.level = 0.9)
condition.context.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location!="Rural"],conf.level = 0.95)
condition.subvsurban.spending.90 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.spending.95 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvssub.spending.90 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.spending.95 <- t.test(a$spending[a$condition_location=="Rural"],a$spending[a$condition_location=="Suburban"],conf.level = 0.95)
condition.subvsrural.spending.90 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.spending.95 <- t.test(a$spending[a$condition_location=="Suburban"],a$spending[a$condition_location=="Rural"],conf.level = 0.95)

condition.context.spending.90_nonrural <- t.test(a$spending[a$condition_location=="Rural" & a$classification!="rural"],a$spending[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.9)
condition.context.spending.95_nonrural <- t.test(a$spending[a$condition_location=="Rural" & a$classification!="rural"],a$spending[a$condition_location!="Rural" & a$classification!="rural"],conf.level = 0.95)
condition.context.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.context.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location!="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvsurban.spending.90_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.spending.95_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.spending.90_suburban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.spending.95_suburban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.spending.90_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.spending.95_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.spending.90_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.spending.95_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.spending.90_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.spending.95_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.spending.90_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.spending.95_sub <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],a$spending[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.spending.90_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.spending.95_urban <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="urban"],a$spending[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.spending.90_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.spending.95_rural <- t.test(a$spending[a$condition_location=="Suburban" & a$classification=="rural"],a$spending[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.spending.90_sub <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.spending.95_sub <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="suburban"],a$spending[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.spending.90_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.spending.95_urban <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="urban"],a$spending[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.spending.90_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.spending.95_rural <- t.test(a$spending[a$condition_location=="Rural" & a$classification=="rural"],a$spending[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)


condition.ruralvsurban.punitive.90 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_suburban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_suburban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsurban.punitive.90 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.9)
condition.subvsurban.punitive.95 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Urban"],conf.level = 0.95)
condition.subvsurban.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsurban.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsurban.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.subvsurban.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.subvsurban.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.subvsurban.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)

condition.subvsrural.punitive.90 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Rural"],conf.level = 0.9)
condition.subvsrural.punitive.95 <- t.test(a$enforcement[a$condition_location=="Suburban"],a$enforcement[a$condition_location=="Rural"],conf.level = 0.95)
condition.subvsrural.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.9)
condition.subvsrural.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],conf.level = 0.95)
condition.subvsrural.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.9)
condition.subvsrural.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],conf.level = 0.95)
condition.subvsrural.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.9)
condition.subvsrural.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],conf.level = 0.95)

condition.ruralvssub.punitive.90 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Suburban"],conf.level = 0.9)
condition.ruralvssub.punitive.95 <- t.test(a$enforcement[a$condition_location=="Rural"],a$enforcement[a$condition_location=="Suburban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvssub.punitive.95_urban <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="urban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_sub <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvssub.punitive.95_sub <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="suburban"],a$enforcement[a$condition_location=="Suburban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvssub.punitive.90_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvssub.punitive.95_rural <- t.test(a$enforcement[a$condition_location=="Rural" & a$classification=="rural"],a$enforcement[a$condition_location=="Suburban" & a$classification=="rural"],conf.level = 0.95)


condition.context.spending.90_matching <-  t.test(a$spending[a$context_match==1],a$spending[a$context_match==0],conf.level = 0.90)
condition.context.spending.95_matching <-  t.test(a$spending[a$context_match==1],a$spending[a$context_match==0],conf.level = 0.95)

condition.context.punitive.90_matching <-  t.test(a$enforcement[a$context_match==1],a$enforcement[a$context_match==0],conf.level = 0.90)
condition.context.punitive.95_matching <-  t.test(a$enforcement[a$context_match==1],a$enforcement[a$context_match==0],conf.level = 0.95)

condition.ruralvsurban.blame.90 <- t.test(a$indiv_blame[a$condition_location=="Rural"],a$indiv_blame[a$condition_location=="Urban"],conf.level = 0.9)
condition.ruralvsurban.blame.95 <- t.test(a$indiv_blame[a$condition_location=="Rural"],a$indiv_blame[a$condition_location=="Urban"],conf.level = 0.95)
condition.ruralvsurban.blame.90_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.9)
condition.ruralvsurban.blame.95_urban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="urban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="urban"],conf.level = 0.95)
condition.ruralvsurban.blame.90_suburban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.9)
condition.ruralvsurban.blame.95_suburban <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="suburban"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="suburban"],conf.level = 0.95)
condition.ruralvsurban.blame.90_rural <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.9)
condition.ruralvsurban.blame.95_rural <- t.test(a$indiv_blame[a$condition_location=="Rural" & a$classification=="rural"],a$indiv_blame[a$condition_location=="Urban" & a$classification=="rural"],conf.level = 0.95)
condition.context.blame.90_matching <-  t.test(a$indiv_blame[a$context_match==1],a$indiv_blame[a$context_match==0],conf.level = 0.90)
condition.context.blame.95_matching <-  t.test(a$indiv_blame[a$context_match==1],a$indiv_blame[a$context_match==0],conf.level = 0.95)


treatment_tab <- data.frame(
  rbind(
    c((condition.ruralvsurban.spending.95_urban$estimate[1]-condition.ruralvsurban.spending.95_urban$estimate[2]),condition.ruralvsurban.spending.95_urban$conf.int[1],condition.ruralvsurban.spending.95_urban$conf.int[2],condition.ruralvsurban.spending.90_urban$conf.int[1],condition.ruralvsurban.spending.90_urban$conf.int[2]),
    c((condition.ruralvsurban.spending.95_suburban$estimate[1]-condition.ruralvsurban.spending.95_suburban$estimate[2]),condition.ruralvsurban.spending.95_suburban$conf.int[1],condition.ruralvsurban.spending.95_suburban$conf.int[2],condition.ruralvsurban.spending.90_suburban$conf.int[1],condition.ruralvsurban.spending.90_suburban$conf.int[2]),
    c((condition.ruralvsurban.spending.95_rural$estimate[1]-condition.ruralvsurban.spending.95_rural$estimate[2]),condition.ruralvsurban.spending.95_rural$conf.int[1],condition.ruralvsurban.spending.95_rural$conf.int[2],condition.ruralvsurban.spending.90_rural$conf.int[1],condition.ruralvsurban.spending.90_rural$conf.int[2]),
    c((condition.ruralvsurban.spending.95$estimate[1]-condition.ruralvsurban.spending.95$estimate[2]),condition.ruralvsurban.spending.95$conf.int[1],condition.ruralvsurban.spending.95$conf.int[2],condition.ruralvsurban.spending.90$conf.int[1],condition.ruralvsurban.spending.90$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$comparison <- "Rural - urban"
treatment_tab$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab$dv <- "Treatment Spending\n(interval scale)"

treatment_tab_punitive <- data.frame(
  rbind(
    c((condition.ruralvsurban.punitive.95_urban$estimate[1]-condition.ruralvsurban.punitive.95_urban$estimate[2]),condition.ruralvsurban.punitive.95_urban$conf.int[1],condition.ruralvsurban.punitive.95_urban$conf.int[2],condition.ruralvsurban.punitive.90_urban$conf.int[1],condition.ruralvsurban.punitive.90_urban$conf.int[2]),
    c((condition.ruralvsurban.punitive.95_suburban$estimate[1]-condition.ruralvsurban.punitive.95_suburban$estimate[2]),condition.ruralvsurban.punitive.95_suburban$conf.int[1],condition.ruralvsurban.punitive.95_suburban$conf.int[2],condition.ruralvsurban.punitive.90_suburban$conf.int[1],condition.ruralvsurban.punitive.90_suburban$conf.int[2]),
    c((condition.ruralvsurban.punitive.95_rural$estimate[1]-condition.ruralvsurban.punitive.95_rural$estimate[2]),condition.ruralvsurban.punitive.95_rural$conf.int[1],condition.ruralvsurban.punitive.95_rural$conf.int[2],condition.ruralvsurban.punitive.90_rural$conf.int[1],condition.ruralvsurban.punitive.90_rural$conf.int[2]),
    c((condition.ruralvsurban.punitive.95$estimate[1]-condition.ruralvsurban.punitive.95$estimate[2]),condition.ruralvsurban.punitive.95$conf.int[1],condition.ruralvsurban.punitive.95$conf.int[2],condition.ruralvsurban.punitive.90$conf.int[1],condition.ruralvsurban.punitive.90$conf.int[2])
  )
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$comparison <- "Rural - urban"
treatment_tab_punitive$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive$dv <- "Enforcement Spending\n(interval scale)"

treatment_tab_subvsurb <- data.frame(
  rbind(
    c((condition.subvsurban.spending.95_urban$estimate[1]-condition.subvsurban.spending.95_urban$estimate[2]),condition.subvsurban.spending.95_urban$conf.int[1],condition.subvsurban.spending.95_urban$conf.int[2],condition.subvsurban.spending.90_urban$conf.int[1],condition.subvsurban.spending.90_urban$conf.int[2]),
    c((condition.subvsurban.spending.95_sub$estimate[1]-condition.subvsurban.spending.95_sub$estimate[2]),condition.subvsurban.spending.95_sub$conf.int[1],condition.subvsurban.spending.95_sub$conf.int[2],condition.subvsurban.spending.90_sub$conf.int[1],condition.subvsurban.spending.90_sub$conf.int[2]),
    c((condition.subvsurban.spending.95_rural$estimate[1]-condition.subvsurban.spending.95_rural$estimate[2]),condition.subvsurban.spending.95_rural$conf.int[1],condition.subvsurban.spending.95_rural$conf.int[2],condition.subvsurban.spending.90_rural$conf.int[1],condition.subvsurban.spending.90_rural$conf.int[2]),
    c((condition.subvsurban.spending.95$estimate[1]-condition.subvsurban.spending.95$estimate[2]),condition.subvsurban.spending.95$conf.int[1],condition.subvsurban.spending.95$conf.int[2],condition.subvsurban.spending.90$conf.int[1],condition.subvsurban.spending.90$conf.int[2])
  )
)
names(treatment_tab_subvsurb) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_subvsurb$comparison <- "Suburban - urban"
treatment_tab_subvsurb$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_subvsurb$dv <- "Treatment Spending\n(interval scale)"

treatment_tab_punitive_subvsurb <- data.frame(
  rbind(
    c((condition.subvsurban.punitive.95_urban$estimate[1]-condition.subvsurban.punitive.95_urban$estimate[2]),condition.subvsurban.punitive.95_urban$conf.int[1],condition.subvsurban.punitive.95_urban$conf.int[2],condition.subvsurban.punitive.90_urban$conf.int[1],condition.subvsurban.punitive.90_urban$conf.int[2]),
    c((condition.subvsurban.punitive.95_sub$estimate[1]-condition.subvsurban.punitive.95_sub$estimate[2]),condition.subvsurban.punitive.95_sub$conf.int[1],condition.subvsurban.punitive.95_sub$conf.int[2],condition.subvsurban.punitive.90_sub$conf.int[1],condition.subvsurban.punitive.90_sub$conf.int[2]),
    c((condition.subvsurban.punitive.95_rural$estimate[1]-condition.subvsurban.punitive.95_rural$estimate[2]),condition.subvsurban.punitive.95_rural$conf.int[1],condition.subvsurban.punitive.95_rural$conf.int[2],condition.subvsurban.punitive.90_rural$conf.int[1],condition.subvsurban.punitive.90_rural$conf.int[2]),
    c((condition.subvsurban.punitive.95$estimate[1]-condition.subvsurban.punitive.95$estimate[2]),condition.subvsurban.punitive.95$conf.int[1],condition.subvsurban.punitive.95$conf.int[2],condition.subvsurban.punitive.90$conf.int[1],condition.subvsurban.punitive.90$conf.int[2])
  )
)
names(treatment_tab_punitive_subvsurb) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_subvsurb$comparison <- "Suburban - urban"
treatment_tab_punitive_subvsurb$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_subvsurb$dv <- "Enforcement Spending\n(interval scale)"

treatment_tab_subvsrural <- data.frame(
  rbind(
    c((condition.subvsrural.spending.95_urban$estimate[1]-condition.subvsrural.spending.95_urban$estimate[2]),condition.subvsrural.spending.95_urban$conf.int[1],condition.subvsrural.spending.95_urban$conf.int[2],condition.subvsrural.spending.90_urban$conf.int[1],condition.subvsrural.spending.90_urban$conf.int[2]),
    c((condition.subvsrural.spending.95_sub$estimate[1]-condition.subvsrural.spending.95_sub$estimate[2]),condition.subvsrural.spending.95_sub$conf.int[1],condition.subvsrural.spending.95_sub$conf.int[2],condition.subvsrural.spending.90_sub$conf.int[1],condition.subvsrural.spending.90_sub$conf.int[2]),
    c((condition.subvsrural.spending.95_rural$estimate[1]-condition.subvsrural.spending.95_rural$estimate[2]),condition.subvsrural.spending.95_rural$conf.int[1],condition.subvsrural.spending.95_rural$conf.int[2],condition.subvsrural.spending.90_rural$conf.int[1],condition.subvsrural.spending.90_rural$conf.int[2]),
    c((condition.subvsrural.spending.95$estimate[1]-condition.subvsrural.spending.95$estimate[2]),condition.subvsrural.spending.95$conf.int[1],condition.subvsrural.spending.95$conf.int[2],condition.subvsrural.spending.90$conf.int[1],condition.subvsrural.spending.90$conf.int[2])
  )
)
names(treatment_tab_subvsrural) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_subvsrural$comparison <- "Suburban - rural"
treatment_tab_subvsrural$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_subvsrural$dv <- "Treatment Spending\n(interval scale)"

treatment_tab_punitive_subvsrural <- data.frame(
  rbind(
    c((condition.subvsrural.punitive.95_urban$estimate[1]-condition.subvsrural.punitive.95_urban$estimate[2]),condition.subvsrural.punitive.95_urban$conf.int[1],condition.subvsrural.punitive.95_urban$conf.int[2],condition.subvsrural.punitive.90_urban$conf.int[1],condition.subvsrural.punitive.90_urban$conf.int[2]),
    c((condition.subvsrural.punitive.95_sub$estimate[1]-condition.subvsrural.punitive.95_sub$estimate[2]),condition.subvsrural.punitive.95_sub$conf.int[1],condition.subvsrural.punitive.95_sub$conf.int[2],condition.subvsrural.punitive.90_sub$conf.int[1],condition.subvsrural.punitive.90_sub$conf.int[2]),
    c((condition.subvsrural.punitive.95_rural$estimate[1]-condition.subvsrural.punitive.95_rural$estimate[2]),condition.subvsrural.punitive.95_rural$conf.int[1],condition.subvsrural.punitive.95_rural$conf.int[2],condition.subvsrural.punitive.90_rural$conf.int[1],condition.subvsrural.punitive.90_rural$conf.int[2]),
    c((condition.subvsrural.punitive.95$estimate[1]-condition.subvsrural.punitive.95$estimate[2]),condition.subvsrural.punitive.95$conf.int[1],condition.subvsrural.punitive.95$conf.int[2],condition.subvsrural.punitive.90$conf.int[1],condition.subvsrural.punitive.90$conf.int[2])
  )
)
names(treatment_tab_punitive_subvsrural) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_subvsrural$comparison <- "Suburban - rural"
treatment_tab_punitive_subvsrural$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_subvsrural$dv <- "Enforcement Spending\n(interval scale)"

treatment_tab_ruralvssub <- data.frame(
  rbind(
    c((condition.ruralvssub.spending.95_urban$estimate[1]-condition.ruralvssub.spending.95_urban$estimate[2]),condition.ruralvssub.spending.95_urban$conf.int[1],condition.ruralvssub.spending.95_urban$conf.int[2],condition.ruralvssub.spending.90_urban$conf.int[1],condition.ruralvssub.spending.90_urban$conf.int[2]),
    c((condition.ruralvssub.spending.95_sub$estimate[1]-condition.ruralvssub.spending.95_sub$estimate[2]),condition.ruralvssub.spending.95_sub$conf.int[1],condition.ruralvssub.spending.95_sub$conf.int[2],condition.ruralvssub.spending.90_sub$conf.int[1],condition.ruralvssub.spending.90_sub$conf.int[2]),
    c((condition.ruralvssub.spending.95_rural$estimate[1]-condition.ruralvssub.spending.95_rural$estimate[2]),condition.ruralvssub.spending.95_rural$conf.int[1],condition.ruralvssub.spending.95_rural$conf.int[2],condition.ruralvssub.spending.90_rural$conf.int[1],condition.ruralvssub.spending.90_rural$conf.int[2]),
    c((condition.ruralvssub.spending.95$estimate[1]-condition.ruralvssub.spending.95$estimate[2]),condition.ruralvssub.spending.95$conf.int[1],condition.ruralvssub.spending.95$conf.int[2],condition.ruralvssub.spending.90$conf.int[1],condition.ruralvssub.spending.90$conf.int[2])
  )
)
names(treatment_tab_ruralvssub) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_ruralvssub$comparison <- "Rural - suburban"
treatment_tab_ruralvssub$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_ruralvssub$dv <- "Treatment Spending\n(interval scale)"

treatment_tab_punitive_ruralvssub <- data.frame(
  rbind(
    c((condition.ruralvssub.punitive.95_urban$estimate[1]-condition.ruralvssub.punitive.95_urban$estimate[2]),condition.ruralvssub.punitive.95_urban$conf.int[1],condition.ruralvssub.punitive.95_urban$conf.int[2],condition.ruralvssub.punitive.90_urban$conf.int[1],condition.ruralvssub.punitive.90_urban$conf.int[2]),
    c((condition.ruralvssub.punitive.95_sub$estimate[1]-condition.ruralvssub.punitive.95_sub$estimate[2]),condition.ruralvssub.punitive.95_sub$conf.int[1],condition.ruralvssub.punitive.95_sub$conf.int[2],condition.ruralvssub.punitive.90_sub$conf.int[1],condition.ruralvssub.punitive.90_sub$conf.int[2]),
    c((condition.ruralvssub.punitive.95_rural$estimate[1]-condition.ruralvssub.punitive.95_rural$estimate[2]),condition.ruralvssub.punitive.95_rural$conf.int[1],condition.ruralvssub.punitive.95_rural$conf.int[2],condition.ruralvssub.punitive.90_rural$conf.int[1],condition.ruralvssub.punitive.90_rural$conf.int[2]),
    c((condition.ruralvssub.punitive.95$estimate[1]-condition.ruralvssub.punitive.95$estimate[2]),condition.ruralvssub.punitive.95$conf.int[1],condition.ruralvssub.punitive.95$conf.int[2],condition.ruralvssub.punitive.90$conf.int[1],condition.ruralvssub.punitive.90$conf.int[2])
  )
)
names(treatment_tab_punitive_ruralvssub) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive_ruralvssub$comparison <- "Rural - suburban"
treatment_tab_punitive_ruralvssub$Subset <- factor(c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample"))
treatment_tab_punitive_ruralvssub$dv <- "Enforcement Spending\n(interval scale)"


variables <- bind_rows(treatment_tab,treatment_tab_punitive,treatment_tab_subvsurb,treatment_tab_punitive_subvsurb,treatment_tab_ruralvssub,treatment_tab_punitive_ruralvssub)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(plot_treat <- ggplot(variables, aes(x=Subset,group=comparison)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),position=position_dodge(width=0.5),
                data=variables,
                width=.0, size=0.75) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),position=position_dodge(width=0.5),
                data=variables,
                width=.0, size=1.5) +
  facet_wrap(facets = "dv",nrow = 1) + 
  geom_point(aes(y=treatment_effect,shape=comparison),position=position_dodge(width=0.5),
             data=variables,size=3) + 
  geom_hline(aes(yintercept=0),lty=2) + 
  # xlab(NULL) +
  ylab("Treatment effect of substance user\nresidential context on policy support") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.15,0.15)) +
  scale_x_discrete(limits=c("Urban\nRespondents","Suburban\nRespondents","Rural\nRespondents","Full\nSample")) + 
  scale_shape_discrete("") + 
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15),
        legend.position = "bottom")
)

# fname = "context_2dvs_treatment_resploc_3comparisons_continuous.pdf"
fname = "Figure5.pdf"
CairoPDF(fname, width=7, height=5.5)
print(plot_treat)
dev.off()


# pvalues for q value
p_vals_fig5 <- c(condition.subvsurban.spending.95_rural$p.value,
                 condition.ruralvsurban.spending.95_rural$p.value,
                 condition.ruralvssub.spending.95_rural$p.value,
                 condition.subvsurban.spending.95_sub$p.value,
                 condition.ruralvsurban.spending.95_suburban$p.value,
                 condition.ruralvssub.spending.95_sub$p.value,
                 condition.subvsurban.spending.95_urban$p.value,
                 condition.ruralvsurban.spending.95_urban$p.value,
                 condition.ruralvssub.spending.95_urban$p.value,
                 condition.subvsurban.punitive.95_rural$p.value,
                 condition.ruralvsurban.punitive.95_rural$p.value,
                 condition.ruralvssub.punitive.95_rural$p.value,
                 condition.subvsurban.punitive.95_sub$p.value,
                 condition.ruralvsurban.punitive.95_suburban$p.value,
                 condition.ruralvssub.punitive.95_sub$p.value,
                 condition.subvsurban.punitive.95_urban$p.value,
                 condition.ruralvsurban.punitive.95_urban$p.value,
                 condition.ruralvssub.punitive.95_urban$p.value)


##### Heterogeneous by resp gender, cont outcome -------------------------------
condition.gender.spending.90 <- t.test(a$spending[a$condition_gender=="Male"],a$spending[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.spending.95 <- t.test(a$spending[a$condition_gender=="Male"],a$spending[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.spending.90_female <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1],a$spending[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.spending.95_female <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1],a$spending[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.spending.90_male <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0],a$spending[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.spending.95_male <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0],a$spending[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)

condition.gender.spending.90_matching <-  t.test(a$spending[a$gender_match==1],a$spending[a$gender_match==0],conf.level = 0.90)
condition.gender.spending.95_matching <-  t.test(a$spending[a$gender_match==1],a$spending[a$gender_match==0],conf.level = 0.95)

condition.gender.punitive.90 <- t.test(a$enforcement[a$condition_gender=="Male"],a$enforcement[a$condition_gender=="Female"],conf.level = 0.9)
condition.gender.punitive.95 <- t.test(a$enforcement[a$condition_gender=="Male"],a$enforcement[a$condition_gender=="Female"],conf.level = 0.95)
condition.gender.punitive.90_female <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.9)
condition.gender.punitive.95_female <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1],conf.level = 0.95)
condition.gender.punitive.90_male <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.9)
condition.gender.punitive.95_male <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0],conf.level = 0.95)
condition.gender.punitive.90_matching <-  t.test(a$enforcement[a$gender_match==1],a$enforcement[a$gender_match==0],conf.level = 0.90)
condition.gender.punitive.95_matching <-  t.test(a$enforcement[a$gender_match==1],a$enforcement[a$gender_match==0],conf.level = 0.95)


## both funding measures:
treatment_tab <- data.frame(
  rbind(
    c((condition.gender.spending.95_female$estimate[1]-condition.gender.spending.95_female$estimate[2]),condition.gender.spending.95_female$conf.int[1],condition.gender.spending.95_female$conf.int[2],condition.gender.spending.90_female$conf.int[1],condition.gender.spending.90_female$conf.int[2]),
    c((condition.gender.spending.95_male$estimate[1]-condition.gender.spending.95_male$estimate[2]),condition.gender.spending.95_male$conf.int[1],condition.gender.spending.95_male$conf.int[2],condition.gender.spending.90_male$conf.int[1],condition.gender.spending.90_male$conf.int[2]),
    c((condition.gender.spending.95$estimate[1]-condition.gender.spending.95$estimate[2]),condition.gender.spending.95$conf.int[1],condition.gender.spending.95$conf.int[2],condition.gender.spending.90$conf.int[1],condition.gender.spending.90$conf.int[2]),
    
    c((condition.gender.punitive.95_female$estimate[1]-condition.gender.punitive.95_female$estimate[2]),condition.gender.punitive.95_female$conf.int[1],condition.gender.punitive.95_female$conf.int[2],condition.gender.punitive.90_female$conf.int[1],condition.gender.punitive.90_female$conf.int[2]),
    c((condition.gender.punitive.95_male$estimate[1]-condition.gender.punitive.95_male$estimate[2]),condition.gender.punitive.95_male$conf.int[1],condition.gender.punitive.95_male$conf.int[2],condition.gender.punitive.90_male$conf.int[1],condition.gender.punitive.90_male$conf.int[2]),
    c((condition.gender.punitive.95$estimate[1]-condition.gender.punitive.95$estimate[2]),condition.gender.punitive.95$conf.int[1],condition.gender.punitive.95$conf.int[2],condition.gender.punitive.90$conf.int[1],condition.gender.punitive.90$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Subset <- factor(c("Women\nRespondents","Men\nRespondents","Full\nSample"))
treatment_tab$outcome <- factor(rep(c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),each=3),
                                levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),
                                ordered = T)



(plot_treat <- ggplot(treatment_tab, aes(x=Subset)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
                data=treatment_tab,
                width=.0, size=0.75) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
                data=treatment_tab,
                width=.0, size=1.5) +
  geom_point(aes(y=treatment_effect),
             data=treatment_tab,size=3) + 
  facet_wrap(~outcome, nrow=1) + 
  geom_hline(aes(yintercept=0),lty=2) + 
  # xlab(NULL) +
  ylab("Treatment effect of substance user gender\n(man condition - woman condition) on policy support") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.1,0.1)) +
  scale_x_discrete(limits=c("Women\nRespondents","Men\nRespondents","Full\nSample")) + 
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15))
)

## Figure 4:
# fname = "gender_both_treatment_respgender_continuous.pdf"
fname = "Figure4.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat)
dev.off()


# pvalues
p_vals_fig4 <- c(condition.gender.spending.95_male$p.value, condition.gender.spending.95_female$p.value,
                 condition.gender.punitive.95_male$p.value, condition.gender.punitive.95_female$p.value)



##### Identity matching, cont outcome ------------------------------------------
treatment_tab <- data.frame(
  rbind(
    c((condition.race.spending.95_matching$estimate[1]-condition.race.spending.95_matching$estimate[2]),condition.race.spending.95_matching$conf.int[1],condition.race.spending.95_matching$conf.int[2],condition.race.spending.90_matching$conf.int[1],condition.race.spending.90_matching$conf.int[2]),
    c((condition.gender.spending.95_matching$estimate[1]-condition.gender.spending.95_matching$estimate[2]),condition.gender.spending.95_matching$conf.int[1],condition.gender.spending.95_matching$conf.int[2],condition.gender.spending.90_matching$conf.int[1],condition.gender.spending.90_matching$conf.int[2]),
    c((condition.context.spending.95_matching$estimate[1]-condition.context.spending.95_matching$estimate[2]),condition.context.spending.95_matching$conf.int[1],condition.context.spending.95_matching$conf.int[2],condition.context.spending.90_matching$conf.int[1],condition.context.spending.90_matching$conf.int[2]),
    
    c((condition.race.punitive.95_matching$estimate[1]-condition.race.punitive.95_matching$estimate[2]),condition.race.punitive.95_matching$conf.int[1],condition.race.punitive.95_matching$conf.int[2],condition.race.punitive.90_matching$conf.int[1],condition.race.punitive.90_matching$conf.int[2]),
    c((condition.gender.punitive.95_matching$estimate[1]-condition.gender.punitive.95_matching$estimate[2]),condition.gender.punitive.95_matching$conf.int[1],condition.gender.punitive.95_matching$conf.int[2],condition.gender.punitive.90_matching$conf.int[1],condition.gender.punitive.90_matching$conf.int[2]),
    c((condition.context.punitive.95_matching$estimate[1]-condition.context.punitive.95_matching$estimate[2]),condition.context.punitive.95_matching$conf.int[1],condition.context.punitive.95_matching$conf.int[2],condition.context.punitive.90_matching$conf.int[1],condition.context.punitive.90_matching$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Manipulation <- factor(rep(c("Race","Gender","Residential\nContext"),times=2))
treatment_tab$outcome <- factor(rep(c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),each=3),
                                levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),
                                ordered = T)

(plot_treat_matching_all <- ggplot(treatment_tab,aes(x=Manipulation)) +
  geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),
                data=treatment_tab,
                width=.0, size=0.75) +
  geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),
                data=treatment_tab,
                width=.0, size=1.5) +
  geom_point(aes(y=treatment_effect),
             data=treatment_tab,
             size=3) +
  geom_hline(aes(yintercept=0),lty=2) + 
  facet_wrap(~ outcome,nrow=1) + 
  # xlab(NULL) +
  ylab("Treatment effect of substance user attribute\n(identity matching - non-matching) on policy support") +
  theme_bw() + 
  scale_y_continuous(limits=c(-0.05,0.085)) + 
  scale_x_discrete(limits=c("Residential\nContext","Gender","Race")) +
  coord_flip() +
  theme(text=element_text(colour=black,
                          # family="CM Roman",
                          size=15))
)

# fname = "identity_2dvs_treatment_matching_continuous.pdf"
fname = "Figure6.pdf"
CairoPDF(fname, width=7, height=4)
print(plot_treat_matching_all)
dev.off()



### blame 
treatment_tab <- data.frame(
  rbind(
    c((condition.race.blame.95_matching$estimate[1]-condition.race.blame.95_matching$estimate[2]),condition.race.blame.95_matching$conf.int[1],condition.race.blame.95_matching$conf.int[2],condition.race.blame.90_matching$conf.int[1],condition.race.blame.90_matching$conf.int[2]),
    c((condition.gender.blame.95_matching$estimate[1]-condition.gender.blame.95_matching$estimate[2]),condition.gender.blame.95_matching$conf.int[1],condition.gender.blame.95_matching$conf.int[2],condition.gender.blame.90_matching$conf.int[1],condition.gender.blame.90_matching$conf.int[2]),
    c((condition.context.blame.95_matching$estimate[1]-condition.context.blame.95_matching$estimate[2]),condition.context.blame.95_matching$conf.int[1],condition.context.blame.95_matching$conf.int[2],condition.context.blame.90_matching$conf.int[1],condition.context.blame.90_matching$conf.int[2])
  )
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Manipulation <- factor(rep(c("Race","Gender","Residential\nContext")))
treatment_tab$outcome <- factor(rep("Individual Blame\n(interval scale)",each=3),
                                ordered = T)


# p values for q values
p_vals_fig6 <- c(condition.race.spending.95_matching$p.value,
                 condition.gender.spending.95_matching$p.value,
                 condition.context.spending.95_matching$p.value,
                 condition.race.punitive.95_matching$p.value,
                 condition.gender.punitive.95_matching$p.value,
                 condition.context.punitive.95_matching$p.value)


##### Identity matching, cont outcome, by pathway ------------------------------------------
fit.race.spending.matching.heroin <- lm(spending ~ race_match,data=filter(a,condition_pathway=="Heroin"))
fit.race.spending.matching.illegalpills <- lm(spending ~ race_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.race.spending.matching.prescriptionpills <- lm(spending ~ race_match,data=filter(a,condition_pathway=="Prescription pills"))

fit.race.punitive.matching.heroin <- lm(enforcement ~ race_match,data=filter(a,condition_pathway=="Heroin"))
fit.race.punitive.matching.illegalpills <- lm(enforcement ~ race_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.race.punitive.matching.prescriptionpills <- lm(enforcement ~ race_match,data=filter(a,condition_pathway=="Prescription pills"))

fit.gender.spending.matching.heroin <- lm(spending ~ gender_match,data=filter(a,condition_pathway=="Heroin"))
fit.gender.spending.matching.illegalpills <- lm(spending ~ gender_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.gender.spending.matching.prescriptionpills <- lm(spending ~ gender_match,data=filter(a,condition_pathway=="Prescription pills"))

fit.gender.punitive.matching.heroin <- lm(enforcement ~ gender_match,data=filter(a,condition_pathway=="Heroin"))
fit.gender.punitive.matching.illegalpills <- lm(enforcement ~ gender_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.gender.punitive.matching.prescriptionpills <- lm(enforcement ~ gender_match,data=filter(a,condition_pathway=="Prescription pills"))

fit.location.spending.matching.heroin <- lm(spending ~ context_match,data=filter(a,condition_pathway=="Heroin"))
fit.location.spending.matching.illegalpills <- lm(spending ~ context_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.location.spending.matching.prescriptionpills <- lm(spending ~ context_match,data=filter(a,condition_pathway=="Prescription pills"))

fit.location.punitive.matching.heroin <- lm(enforcement ~ context_match,data=filter(a,condition_pathway=="Heroin"))
fit.location.punitive.matching.illegalpills <- lm(enforcement ~ context_match,data=filter(a,condition_pathway=="Illegal pills"))
fit.location.punitive.matching.prescriptionpills <- lm(enforcement ~ context_match,data=filter(a,condition_pathway=="Prescription pills"))

str(summary(fit.race.spending.matching.heroin))

p_vals_fig8 <- c(summary(fit.race.spending.matching.heroin)$coefficients[2,4], 
                 summary(fit.gender.spending.matching.heroin)$coefficients[2,4], 
                 summary(fit.location.spending.matching.heroin)$coefficients[2,4], 
                 summary(fit.race.spending.matching.prescriptionpills)$coefficients[2,4], 
                 summary(fit.gender.spending.matching.prescriptionpills)$coefficients[2,4], 
                 summary(fit.location.spending.matching.prescriptionpills)$coefficients[2,4],
                 summary(fit.race.punitive.matching.heroin)$coefficients[2,4], 
                 summary(fit.gender.punitive.matching.heroin)$coefficients[2,4], 
                 summary(fit.location.punitive.matching.heroin)$coefficients[2,4], 
                 summary(fit.race.punitive.matching.prescriptionpills)$coefficients[2,4], 
                 summary(fit.gender.punitive.matching.prescriptionpills)$coefficients[2,4], 
                 summary(fit.location.punitive.matching.prescriptionpills)$coefficients[2,4])

  
  
treatment_tab <- bind_rows(
  tidy(fit.race.spending.matching.heroin) %>% mutate(Manipulation="Race", Outcome="Treatment Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.race.spending.matching.illegalpills) %>% mutate(Manipulation="Race", Outcome="Treatment Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.race.spending.matching.prescriptionpills) %>% mutate(Manipulation="Race", Outcome="Treatment Spending\n(interval scale)",Pathway="Prescription Pills"),
  tidy(fit.race.punitive.matching.heroin) %>% mutate(Manipulation="Race", Outcome="Enforcement Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.race.punitive.matching.illegalpills) %>% mutate(Manipulation="Race", Outcome="Enforcement Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.race.punitive.matching.prescriptionpills) %>% mutate(Manipulation="Race", Outcome="Enforcement Spending\n(interval scale)",Pathway="Prescription Pills"),
  
  tidy(fit.gender.spending.matching.heroin) %>% mutate(Manipulation="Gender", Outcome="Treatment Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.gender.spending.matching.illegalpills) %>% mutate(Manipulation="Gender", Outcome="Treatment Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.gender.spending.matching.prescriptionpills) %>% mutate(Manipulation="Gender", Outcome="Treatment Spending\n(interval scale)",Pathway="Prescription Pills"),
  tidy(fit.gender.punitive.matching.heroin) %>% mutate(Manipulation="Gender", Outcome="Enforcement Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.gender.punitive.matching.illegalpills) %>% mutate(Manipulation="Gender", Outcome="Enforcement Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.gender.punitive.matching.prescriptionpills) %>% mutate(Manipulation="Gender", Outcome="Enforcement Spending\n(interval scale)",Pathway="Prescription Pills"),
  
  tidy(fit.location.spending.matching.heroin) %>% mutate(Manipulation="Residential\nContext", Outcome="Treatment Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.location.spending.matching.illegalpills) %>% mutate(Manipulation="Residential\nContext", Outcome="Treatment Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.location.spending.matching.prescriptionpills) %>% mutate(Manipulation="Residential\nContext", Outcome="Treatment Spending\n(interval scale)",Pathway="Prescription Pills"),
  tidy(fit.location.punitive.matching.heroin) %>% mutate(Manipulation="Residential\nContext", Outcome="Enforcement Spending\n(interval scale)",Pathway="Heroin"),
  tidy(fit.location.punitive.matching.illegalpills) %>% mutate(Manipulation="Residential\nContext", Outcome="Enforcement Spending\n(interval scale)",Pathway="Illegal Pills"),
  tidy(fit.location.punitive.matching.prescriptionpills) %>% mutate(Manipulation="Residential\nContext", Outcome="Enforcement Spending\n(interval scale)",Pathway="Prescription Pills")
) %>%
  mutate(cilo = estimate + std.error*qnorm(0.025),
         cihi = estimate + std.error*qnorm(0.975),
         cilo_90 = estimate + std.error*qnorm(0.05),
         cihi_90 = estimate + std.error*qnorm(0.95)) %>%
  mutate(Manipulation = factor(Manipulation,levels=c("Residential\nContext","Gender","Race"),ordered=T),
         Outcome = factor(Outcome,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),
                          ordered = T)
  )

subset(treatment_tab, term == "gender_match")





(plot_treat_matching_heroin_pills <- ggplot(filter(treatment_tab, Pathway!="Illegal Pills"),aes(x=Manipulation)) +
    geom_errorbar(aes(ymin=cilo, ymax=cihi),
                  width=.0, size=0.75) +
    geom_errorbar(aes(ymin=cilo_90, ymax=cihi_90),
                  width=.0, size=1.5) +
    geom_point(aes(y=estimate),
               size=3) +
    geom_hline(aes(yintercept=0),lty=2) + 
    # facet_wrap( ~ Outcome,nrow=1) + 
    facet_grid(Pathway ~ Outcome) + 
    # xlab(NULL) +
    ylab("Treatment effect of substance user attribute\n(identity matching - non-matching) on policy support") +
    theme_bw() + 
    scale_y_continuous(limits=c(-0.085,0.105)) + 
    scale_x_discrete(limits=c("Residential\nContext","Gender","Race")) +
    coord_flip() +
    theme(text=element_text(colour=black,
                            # family="CM Roman",
                            size=15))
)

# fname = "identity_2dvs_treatment_matching_continuous_heroin_pills.pdf"
fname = "Figure8.pdf"
CairoPDF(fname, width=7, height=7)
print(plot_treat_matching_heroin_pills)
dev.off()


####  Full interaction of experimental variables ####
### Condition Location x (Race x Gender)
## Spending
## Treatment spending
# Condition Location: Urban
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Urban"))
summary(reg)
p_vals_figI11a <- summary(reg)$coefficients[2:3, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Urban)"
variables_treatment_urban <- variables

# Condition Location: Suburban
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Suburban"))
summary(reg)
p_vals_figI11b <- summary(reg)$coefficients[2:3, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Suburban)"
variables_treatment_suburban <- variables

# Condition Location: Rural
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Rural"))
summary(reg)
p_vals_figI11c <- summary(reg)$coefficients[2:3, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Rural)"
variables_treatment_rural <- variables

variables <- bind_rows(variables_treatment_urban,
                       variables_treatment_suburban,
                       variables_treatment_rural)
variables$group <- factor(variables$group, levels = c("Condition Location:\n(Urban)","Condition Location:\n(Suburban)",
                                                      "Condition Location:\n(Rural)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.055, .075),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 6:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.055, .075),ylim=c(1, 6)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_int_location_spending.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureI11.pdf")


## Enforcement
# Condition Location: Urban
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Urban"))
summary(reg)
p_vals_figI12a <- summary(reg)$coefficients[2:3, 4]


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Urban)"
variables_treatment_urban <- variables

# Condition Location: Suburban
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Suburban"))
summary(reg)
p_vals_figI12b <- summary(reg)$coefficients[2:3, 4]


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Suburban)"
variables_treatment_suburban <- variables

# Condition Location: Rural
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")),
          data=subset(a, condition_location == "Rural"))
summary(reg)
p_vals_figI12c <- summary(reg)$coefficients[2:3, 4]


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 6:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Location:\n(Rural)"
variables_treatment_rural <- variables

variables <- bind_rows(variables_treatment_urban,
                       variables_treatment_suburban,
                       variables_treatment_rural)
variables$group <- factor(variables$group, levels = c("Condition Location:\n(Urban)","Condition Location:\n(Suburban)",
                                                      "Condition Location:\n(Rural)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.055, .075),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 6:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.055, .075),ylim=c(1, 6)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_int_location_enforcement.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureI12.pdf")


### Condition Gender x Race
## Spending
# Condition Gender: Male
reg <- lm(spending ~
            factor(condition_race,levels=c("White","Black")), data=subset(a, condition_gender == "Male"))
summary(reg)
p_vals_figI13a <- summary(reg)$coefficients[2,4]

attribute_labels <- c("Condition Race:","    White","    Black")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 3:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Gender:\n(Man)"
variables_treatment_male <- variables

# Condition Gender: Female
reg <- lm(spending ~
            factor(condition_race,levels=c("White","Black")), data=subset(a, condition_gender == "Female"))
summary(reg)
p_vals_figI13b <- summary(reg)$coefficients[2,4]

attribute_labels <- c("Condition Race:","    White","    Black")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 3:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Gender:\n(Woman)"
variables_treatment_female <- variables

variables <- bind_rows(variables_treatment_male,
                       variables_treatment_female)
variables$group <- factor(variables$group, levels = c("Condition Gender:\n(Man)","Condition Gender:\n(Woman)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group",nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.075, .055),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 3:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.075, .055),ylim=c(1, 3)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12)) 
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_int_gender_spending.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureI13.pdf")


## Enforcement
# Condition Gender: Male
reg <- lm(enforcement ~
            factor(condition_race,levels=c("White","Black")), data=subset(a, condition_gender == "Male"))
summary(reg)
p_vals_figI14a <- summary(reg)$coefficients[2,4]

attribute_labels <- c("Condition Race:","    White","    Black")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 3:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Gender:\n(Man)"
variables_treatment_male <- variables

# Condition Gender: Female
reg <- lm(enforcement ~
            factor(condition_race,levels=c("White","Black")), data=subset(a, condition_gender == "Female"))
summary(reg)
p_vals_figI14b <- summary(reg)$coefficients[2,4]

attribute_labels <- c("Condition Race:","    White","    Black")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 3:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Gender:\n(Woman)"
variables_treatment_female <- variables

variables <- bind_rows(variables_treatment_male,
                       variables_treatment_female)
variables$group <- factor(variables$group, levels = c("Condition Gender:\n(Man)","Condition Gender:\n(Woman)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group",nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.075, .055),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 3:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.075, .055),ylim=c(1, 3)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))  
)

# ggsave(cjoint_plot_cont,height=4,width=6,filename = "conjoint_plot_int_gender_enforcement.pdf")
ggsave(cjoint_plot_cont,height=4,width=6,filename = "FigureI14.pdf")




#### Pathway to addiction interaction with everything
## Spending
# Pathway: Heroin
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Heroin"))
summary(reg)
p_vals_figI15a <- summary(reg)$coefficients[2:7, 4]


attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Heroin)"
variables_treatment_heroin <- variables

# Pathway: Illega Pills
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Illegal pills"))
summary(reg)
p_vals_figI15b <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Illegal Pills)"
variables_treatment_pills <- variables


# Pathway: Prescription Pills
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Prescription pills"))
summary(reg)
p_vals_figI15c <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Prescription Pills)"
variables_treatment_prescription <- variables

variables <- bind_rows(variables_treatment_heroin,
                       variables_treatment_pills,
                       variables_treatment_prescription)
variables$group <- factor(variables$group, levels = c("Condition Pathway:\n(Heroin)","Condition Pathway:\n(Illegal Pills)",
                                                      "Condition Pathway:\n(Prescription Pills)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.1, .1),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 14:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.1, .1),ylim=c(1, 14)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=5,width=8,filename = "conjoint_plot_int_pathway_spending.pdf")
ggsave(cjoint_plot_cont,height=5,width=8,filename = "FigureI15.pdf")


## Enforcement
# Pathway: Heroin
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Heroin"))
summary(reg)
p_vals_figI16a <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcment Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Heroin)"
variables_treatment_heroin <- variables

# Pathway: Illega Pills
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Illegal pills"))
summary(reg)
p_vals_figI16b <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Illegal Pills)"
variables_treatment_pills <- variables


# Pathway: Prescription Pills
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_insurance, levels = c("Medicaid", "ACA Marketplace", "Private insurance")),
          data=subset(a, condition_pathway == "Prescription pills"))
summary(reg)
p_vals_figI16c <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Insurance:", "    Medicaid", "    ACA Marketplace", "    Private insurance")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# insurance
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]


variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Pathway:\n(Prescription Pills)"
variables_treatment_prescription <- variables

variables <- bind_rows(variables_treatment_heroin,
                       variables_treatment_pills,
                       variables_treatment_prescription)
variables$group <- factor(variables$group, levels = c("Condition Pathway:\n(Heroin)","Condition Pathway:\n(Illegal Pills)",
                                                      "Condition Pathway:\n(Prescription Pills)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.1, .1),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 14:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.1, .1),ylim=c(1, 14)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))  
)

# ggsave(cjoint_plot_cont,height=5,width=8,filename = "conjoint_plot_int_pathway_enforcement.pdf")
ggsave(cjoint_plot_cont,height=5,width=8,filename = "FigureI16.pdf")


#### Insurance interaction with everything
## Spending
# Insurnace: Medicaid
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "Medicaid"))
summary(reg)
p_vals_figI17a <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")


variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(Medicaid)"
variables_treatment_medicaid <- variables

# Insurance: ACA
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "ACA Marketplace"))
summary(reg)
p_vals_figI17b <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(ACA Marketplace)"
variables_treatment_aca <- variables

# Insurnace: Private
table(a$condition_insurance)
reg <- lm(spending ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "Private insurance"))
summary(reg)
p_vals_figI17c <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Treatment Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(Private Insurance)"
variables_treatment_private <- variables


variables <- bind_rows(variables_treatment_medicaid,
                       variables_treatment_aca,
                       variables_treatment_private)
variables$group <- factor(variables$group, levels = c("Condition Insurance:\n(Medicaid)","Condition Insurance:\n(ACA Marketplace)",
                                                      "Condition Insurance:\n(Private Insurance)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.1, .1),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 14:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.1, .1),ylim=c(1, 14)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=5,width=8,filename = "conjoint_plot_int_insurance_spending.pdf")
ggsave(cjoint_plot_cont,height=5,width=8,filename = "FigureI17.pdf")

## Enforcement
# Insurnace: Medicaid
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "Medicaid"))
summary(reg)
p_vals_figI18a <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(Medicaid)"
variables_treatment_medicaid <- variables

# Insurnace: ACA
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "ACA Marketplace"))
summary(reg)
p_vals_figI18b <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(ACA Marketplace)"
variables_treatment_aca <- variables

# Insurnace: Private
table(a$condition_insurance)
reg <- lm(enforcement ~
            factor(condition_race, levels = c("White", "Black")) + 
            factor(condition_gender, levels = c("Male", "Female")) +
            factor(condition_location, levels = c("Urban", "Suburban", "Rural")) +
            factor(condition_pathway, levels = c("Heroin", "Illegal pills", "Prescription pills")),
          data=subset(a, condition_insurance == "Private insurance"))
summary(reg)
p_vals_figI18c <- summary(reg)$coefficients[2:7, 4]

attribute_labels <- c("Race:","    White","    Black",
                      "Gender:","    Man","    Woman",
                      "Location:", "    Urban", "    Suburban", "    Rural",
                      "Pathway:", "    Heroin", "    Illegal pills", "    Prescription pills")

variables <- data.frame(labels = attribute_labels)
variables$coef <- NA
variables$SE <- NA

# race
variables[2,c(2,3)] <- 0
variables[3,c(2,3)] <- summary(reg)$coefficients[2, c(1,2)]

# gender
variables[5,c(2,3)] <- 0
variables[6,c(2,3)] <- summary(reg)$coefficients[3, c(1,2)]

# location
variables[8,c(2,3)] <- 0
variables[9,c(2,3)] <- summary(reg)$coefficients[4, c(1,2)]
variables[10,c(2,3)] <- summary(reg)$coefficients[5, c(1,2)]

# pathway to addiction
variables[12,c(2,3)] <- 0
variables[13,c(2,3)] <- summary(reg)$coefficients[6, c(1,2)]
variables[14,c(2,3)] <- summary(reg)$coefficients[7, c(1,2)]

variables$scale <- "Continuous"
variables$order <- 14:1 # for ordering plot correctly
variables$fontface <- c("bold","plain","plain","bold","plain","plain","bold","plain","plain","plain","bold","plain","plain","plain")

variables$dv <- "Enforcement Spending\n(interval scale)"
variables$group <- "Condition Insurance:\n(Private Insurance)"
variables_treatment_private <- variables


variables <- bind_rows(variables_treatment_medicaid,
                       variables_treatment_aca,
                       variables_treatment_private)
variables$group <- factor(variables$group, levels = c("Condition Insurance:\n(Medicaid)","Condition Insurance:\n(ACA Marketplace)",
                                                      "Condition Insurance:\n(Private Insurance)"),ordered = T)

(cjoint_plot_cont <- ggplot(data = variables, aes(x = coef, y = order)) +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.025)*SE, 
                                       xmax = coef + qnorm(0.975)*SE), 
                 height=0,lwd=0.75,colour = 'black') +
  geom_errorbarh(data = variables, aes(y = order, 
                                       xmin = coef + qnorm(0.05)*SE, 
                                       xmax = coef + qnorm(0.95)*SE), 
                 height=0, lwd=1.5, colour = 'black') +
  # geom_point(data=subset(variables,coef==0),colour = 'black', size = 2.75,shape=1) + 
  # geom_point(data=subset(variables,coef!=0),colour = 'black', size = 2.75) + 
  geom_point(colour = 'black', size = 2.75) + 
  facet_wrap(facets = "group", nrow = 1) + 
  scale_x_continuous("Difference in policy support from baseline user attribute", limits = c(-.1, .1),breaks = c(-.05,0,.05)) +
  scale_y_continuous("Substance User Attribute",breaks = 14:1,labels=attribute_labels) +
  coord_cartesian(xlim=c(-.1, .1),ylim=c(1, 14)) +
  geom_vline(xintercept = 0,size=.5,colour="black",linetype="dotted") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 0, hjust = 0, color="black")) +  
  theme(axis.text.y = element_text(size=10,face=variables$fontface))  +
  # theme(axis.title.y = element_blank()) +
  theme(axis.text.x = element_text(size=12)) + 
  theme(axis.title.x = element_text(size=12))   
)

# ggsave(cjoint_plot_cont,height=5,width=8,filename = "conjoint_plot_int_insurance_enforcement.pdf")
ggsave(cjoint_plot_cont,height=5,width=8,filename = "FigureI18.pdf")









#### Effect heterogeneity by personal exposure ####
condition.race.spending.90_matching_pers1 <- t.test(a$spending[a$race_match==1 & a$personal_bi==1],a$spending[a$race_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.race.spending.95_matching_pers1 <- t.test(a$spending[a$race_match==1 & a$personal_bi==1],a$spending[a$race_match==0 & a$personal_bi==1],conf.level = 0.95)
condition.race.punitive.90_matching_pers1 <- t.test(a$enforcement[a$race_match==1 & a$personal_bi==1],a$enforcement[a$race_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.race.punitive.95_matching_pers1 <- t.test(a$enforcement[a$race_match==1 & a$personal_bi==1],a$enforcement[a$race_match==0 & a$personal_bi==1],conf.level = 0.95)


condition.gender.spending.90_matching_pers1 <-  t.test(a$spending[a$gender_match==1 & a$personal_bi==1],a$spending[a$gender_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.gender.spending.95_matching_pers1 <-  t.test(a$spending[a$gender_match==1 & a$personal_bi==1],a$spending[a$gender_match==0 & a$personal_bi==1],conf.level = 0.95)
condition.gender.punitive.90_matching_pers1 <-  t.test(a$enforcement[a$gender_match==1 & a$personal_bi==1],a$enforcement[a$gender_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.gender.punitive.95_matching_pers1 <-  t.test(a$enforcement[a$gender_match==1 & a$personal_bi==1],a$enforcement[a$gender_match==0 & a$personal_bi==1],conf.level = 0.95)


condition.context.spending.90_matching_pers1 <-  t.test(a$spending[a$context_match==1 & a$personal_bi==1],a$spending[a$context_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.context.spending.95_matching_pers1 <-  t.test(a$spending[a$context_match==1 & a$personal_bi==1],a$spending[a$context_match==0 & a$personal_bi==1],conf.level = 0.95)
condition.context.punitive.90_matching_pers1 <-  t.test(a$enforcement[a$context_match==1 & a$personal_bi==1],a$enforcement[a$context_match==0 & a$personal_bi==1],conf.level = 0.90)
condition.context.punitive.95_matching_pers1 <-  t.test(a$enforcement[a$context_match==1 & a$personal_bi==1],a$enforcement[a$context_match==0 & a$personal_bi==1],conf.level = 0.95)


condition.race.spending.90_matching_pers0 <- t.test(a$spending[a$race_match==1 & a$personal_bi==0],a$spending[a$race_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.race.spending.95_matching_pers0 <- t.test(a$spending[a$race_match==1 & a$personal_bi==0],a$spending[a$race_match==0 & a$personal_bi==0],conf.level = 0.95)
condition.race.punitive.90_matching_pers0 <- t.test(a$enforcement[a$race_match==1 & a$personal_bi==0],a$enforcement[a$race_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.race.punitive.95_matching_pers0 <- t.test(a$enforcement[a$race_match==1 & a$personal_bi==0],a$enforcement[a$race_match==0 & a$personal_bi==0],conf.level = 0.95)


condition.gender.spending.90_matching_pers0 <-  t.test(a$spending[a$gender_match==1 & a$personal_bi==0],a$spending[a$gender_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.gender.spending.95_matching_pers0 <-  t.test(a$spending[a$gender_match==1 & a$personal_bi==0],a$spending[a$gender_match==0 & a$personal_bi==0],conf.level = 0.95)
condition.gender.punitive.90_matching_pers0 <-  t.test(a$enforcement[a$gender_match==1 & a$personal_bi==0],a$enforcement[a$gender_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.gender.punitive.95_matching_pers0 <-  t.test(a$enforcement[a$gender_match==1 & a$personal_bi==0],a$enforcement[a$gender_match==0 & a$personal_bi==0],conf.level = 0.95)


condition.context.spending.90_matching_pers0 <-  t.test(a$spending[a$context_match==1 & a$personal_bi==0],a$spending[a$context_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.context.spending.95_matching_pers0 <-  t.test(a$spending[a$context_match==1 & a$personal_bi==0],a$spending[a$context_match==0 & a$personal_bi==0],conf.level = 0.95)
condition.context.punitive.90_matching_pers0 <-  t.test(a$enforcement[a$context_match==1 & a$personal_bi==0],a$enforcement[a$context_match==0 & a$personal_bi==0],conf.level = 0.90)
condition.context.punitive.95_matching_pers0 <-  t.test(a$enforcement[a$context_match==1 & a$personal_bi==0],a$enforcement[a$context_match==0 & a$personal_bi==0],conf.level = 0.95)


p_vals_figM23 <- c(condition.race.spending.95_matching_pers1$p.value, condition.race.spending.95_matching_pers0$p.value,
                   condition.gender.spending.95_matching_pers1$p.value, condition.gender.spending.95_matching_pers0$p.value,
                   condition.context.spending.95_matching_pers1$p.value, condition.context.spending.95_matching_pers0$p.value,
                   condition.race.punitive.95_matching_pers1$p.value, condition.race.punitive.95_matching_pers0$p.value,
                   condition.gender.punitive.95_matching_pers1$p.value, condition.gender.punitive.95_matching_pers0$p.value,
                   condition.context.punitive.95_matching_pers1$p.value, condition.context.punitive.95_matching_pers0$p.value)


treatment_tab <- data.frame(
	rbind(
		c((condition.race.spending.95_matching_pers1$estimate[1]-condition.race.spending.95_matching_pers1$estimate[2]),condition.race.spending.95_matching_pers1$conf.int[1],condition.race.spending.95_matching_pers1$conf.int[2],condition.race.spending.90_matching_pers1$conf.int[1],condition.race.spending.90_matching_pers1$conf.int[2]),
		c((condition.gender.spending.95_matching_pers1$estimate[1]-condition.gender.spending.95_matching_pers1$estimate[2]),condition.gender.spending.95_matching_pers1$conf.int[1],condition.gender.spending.95_matching_pers1$conf.int[2],condition.gender.spending.90_matching_pers1$conf.int[1],condition.gender.spending.90_matching_pers1$conf.int[2]),
		c((condition.context.spending.95_matching_pers1$estimate[1]-condition.context.spending.95_matching_pers1$estimate[2]),condition.context.spending.95_matching_pers1$conf.int[1],condition.context.spending.95_matching_pers1$conf.int[2],condition.context.spending.90_matching_pers1$conf.int[1],condition.context.spending.90_matching_pers1$conf.int[2]),
		
		c((condition.race.punitive.95_matching_pers1$estimate[1]-condition.race.punitive.95_matching_pers1$estimate[2]),condition.race.punitive.95_matching_pers1$conf.int[1],condition.race.punitive.95_matching_pers1$conf.int[2],condition.race.punitive.90_matching_pers1$conf.int[1],condition.race.punitive.90_matching_pers1$conf.int[2]),
		c((condition.gender.punitive.95_matching_pers1$estimate[1]-condition.gender.punitive.95_matching_pers1$estimate[2]),condition.gender.punitive.95_matching_pers1$conf.int[1],condition.gender.punitive.95_matching_pers1$conf.int[2],condition.gender.punitive.90_matching_pers1$conf.int[1],condition.gender.punitive.90_matching_pers1$conf.int[2]),
		c((condition.context.punitive.95_matching_pers1$estimate[1]-condition.context.punitive.95_matching_pers1$estimate[2]),condition.context.punitive.95_matching_pers1$conf.int[1],condition.context.punitive.95_matching_pers1$conf.int[2],condition.context.punitive.90_matching_pers1$conf.int[1],condition.context.punitive.90_matching_pers1$conf.int[2]),
		
		c((condition.race.spending.95_matching_pers0$estimate[1]-condition.race.spending.95_matching_pers0$estimate[2]),condition.race.spending.95_matching_pers0$conf.int[1],condition.race.spending.95_matching_pers0$conf.int[2],condition.race.spending.90_matching_pers0$conf.int[1],condition.race.spending.90_matching_pers0$conf.int[2]),
		c((condition.gender.spending.95_matching_pers0$estimate[1]-condition.gender.spending.95_matching_pers0$estimate[2]),condition.gender.spending.95_matching_pers0$conf.int[1],condition.gender.spending.95_matching_pers0$conf.int[2],condition.gender.spending.90_matching_pers0$conf.int[1],condition.gender.spending.90_matching_pers0$conf.int[2]),
		c((condition.context.spending.95_matching_pers0$estimate[1]-condition.context.spending.95_matching_pers0$estimate[2]),condition.context.spending.95_matching_pers0$conf.int[1],condition.context.spending.95_matching_pers0$conf.int[2],condition.context.spending.90_matching_pers0$conf.int[1],condition.context.spending.90_matching_pers0$conf.int[2]),
		
		c((condition.race.punitive.95_matching_pers0$estimate[1]-condition.race.punitive.95_matching_pers0$estimate[2]),condition.race.punitive.95_matching_pers0$conf.int[1],condition.race.punitive.95_matching_pers0$conf.int[2],condition.race.punitive.90_matching_pers0$conf.int[1],condition.race.punitive.90_matching_pers0$conf.int[2]),
		c((condition.gender.punitive.95_matching_pers0$estimate[1]-condition.gender.punitive.95_matching_pers0$estimate[2]),condition.gender.punitive.95_matching_pers0$conf.int[1],condition.gender.punitive.95_matching_pers0$conf.int[2],condition.gender.punitive.90_matching_pers0$conf.int[1],condition.gender.punitive.90_matching_pers0$conf.int[2]),
		c((condition.context.punitive.95_matching_pers0$estimate[1]-condition.context.punitive.95_matching_pers0$estimate[2]),condition.context.punitive.95_matching_pers0$conf.int[1],condition.context.punitive.95_matching_pers0$conf.int[2],condition.context.punitive.90_matching_pers0$conf.int[1],condition.context.punitive.90_matching_pers0$conf.int[2])
		
	)
)

names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$Manipulation <- factor(rep(c("Race","Gender","Residential\nContext"),times=4))
treatment_tab$outcome <- factor(rep(c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),each=3),
																levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),
																ordered = T)
treatment_tab$pers_exp <- factor(rep(c("Know Someone\nw/ Addiction\n","Don't Know Someone\nw/ Addiction"),each=6),
																 levels = c("Know Someone\nw/ Addiction\n","Don't Know Someone\nw/ Addiction"),
																 ordered = T)

(plot_treat_matching_pers_all <- ggplot(treatment_tab,aes(x=Manipulation)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95,group=pers_exp),position=position_dodge(width=0.25),
								data=treatment_tab,
								width=.0, size=0.75) +
	geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90,group=pers_exp),position=position_dodge(width=0.25),
								data=treatment_tab,
								width=.0, size=1.5) +
	geom_point(aes(y=treatment_effect,group=pers_exp,shape=pers_exp),position=position_dodge(width=0.25),
						 data=treatment_tab,
						 size=3) +
	geom_hline(aes(yintercept=0),lty=2) + 
	facet_wrap(~ outcome,nrow=1) + 
	# xlab(NULL) +
	ylab("Treatment effect of recipient attribute\non outcome (identity matching - non-matching)") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.071,0.071)) + 
	scale_x_discrete(limits=c("Residential\nContext","Gender","Race")) +
	scale_shape_discrete("") + 
	coord_flip() +
	theme(text=element_text(colour=black,
													# family="CM Roman",
													size=15),
	      legend.position="bottom")
)

# fname = "identity_2dvs_treatment_matching_pers.pdf"
fname = "FigureM23.pdf"
CairoPDF(fname, width=7, height=6)
print(plot_treat_matching_pers_all)
dev.off()



condition.race.spending.90_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$personal_bi==0],a$spending[a$condition_race=="White" & a$personal_bi==0],conf.level = 0.9)
condition.race.spending.95_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$personal_bi==0],a$spending[a$condition_race=="White" & a$personal_bi==0],conf.level = 0.95)
condition.race.spending.90_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$personal_bi==1],a$spending[a$condition_race=="White" & a$personal_bi==1],conf.level = 0.9)
condition.race.spending.95_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$personal_bi==1],a$spending[a$condition_race=="White" & a$personal_bi==1],conf.level = 0.95)
condition.race.punitive.90_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$personal_bi==0],conf.level = 0.9)
condition.race.punitive.95_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$personal_bi==0],conf.level = 0.95)
condition.race.punitive.90_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$personal_bi==1],conf.level = 0.9)
condition.race.punitive.95_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$personal_bi==1],conf.level = 0.95)

condition.race.spending.90_white_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==0],a$spending[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.race.spending.95_white_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==0],a$spending[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.race.spending.90_white_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==1],a$spending[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.race.spending.95_white_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==1],a$spending[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==1],conf.level = 0.95)
condition.race.punitive.90_white_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.race.punitive.95_white_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.race.punitive.90_white_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.race.punitive.95_white_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$white_resp==1 & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$white_resp==1 & a$personal_bi==1],conf.level = 0.95)


condition.race.spending.90_black_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==0],a$spending[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.race.spending.95_black_pers0 <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==0],a$spending[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.race.spending.90_black_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==1],a$spending[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.race.spending.95_black_pers1 <- t.test(a$spending[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==1],a$spending[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==1],conf.level = 0.95)
condition.race.punitive.90_black_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.race.punitive.95_black_pers0 <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==0],a$enforcement[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.race.punitive.90_black_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.race.punitive.95_black_pers1 <- t.test(a$enforcement[a$condition_race=="Black" & a$black_resp==1 & a$personal_bi==1],a$enforcement[a$condition_race=="White" & a$black_resp==1 & a$personal_bi==1],conf.level = 0.95)

p_vals_figM24 <- c(condition.race.spending.95_pers1$p.value, condition.race.spending.95_pers0$p.value,
                 condition.race.spending.95_black_pers1$p.value, condition.race.spending.95_black_pers0$p.value,
                 condition.race.spending.95_white_pers1$p.value, condition.race.spending.95_white_pers0$p.value,
                 condition.race.punitive.95_pers1$p.value, condition.race.punitive.95_pers0$p.value,
                 condition.race.punitive.95_black_pers1$p.value, condition.race.punitive.95_black_pers0$p.value,
                 condition.race.punitive.95_white_pers1$p.value, condition.race.punitive.95_white_pers0$p.value)

treatment_tab <- data.frame(
	rbind(
		c((condition.race.spending.95_white_pers0$estimate[1]-condition.race.spending.95_white_pers0$estimate[2]),condition.race.spending.95_white_pers0$conf.int[1],condition.race.spending.95_white_pers0$conf.int[2],condition.race.spending.90_white_pers0$conf.int[1],condition.race.spending.90_white_pers0$conf.int[2]),
		c((condition.race.spending.95_white_pers1$estimate[1]-condition.race.spending.95_white_pers1$estimate[2]),condition.race.spending.95_white_pers1$conf.int[1],condition.race.spending.95_white_pers1$conf.int[2],condition.race.spending.90_white_pers1$conf.int[1],condition.race.spending.90_white_pers1$conf.int[2]),
		
		c((condition.race.spending.95_black_pers0$estimate[1]-condition.race.spending.95_black_pers0$estimate[2]),condition.race.spending.95_black_pers0$conf.int[1],condition.race.spending.95_black_pers0$conf.int[2],condition.race.spending.90_black_pers0$conf.int[1],condition.race.spending.90_black_pers0$conf.int[2]),
		c((condition.race.spending.95_black_pers1$estimate[1]-condition.race.spending.95_black_pers1$estimate[2]),condition.race.spending.95_black_pers1$conf.int[1],condition.race.spending.95_black_pers1$conf.int[2],condition.race.spending.90_black_pers1$conf.int[1],condition.race.spending.90_black_pers1$conf.int[2]),
		
		c((condition.race.spending.95_pers0$estimate[1]-condition.race.spending.95_pers0$estimate[2]),condition.race.spending.95_pers0$conf.int[1],condition.race.spending.95_pers0$conf.int[2],condition.race.spending.90_pers0$conf.int[1],condition.race.spending.90_pers0$conf.int[2]),
		c((condition.race.spending.95_pers1$estimate[1]-condition.race.spending.95_pers1$estimate[2]),condition.race.spending.95_pers1$conf.int[1],condition.race.spending.95_pers1$conf.int[2],condition.race.spending.90_pers1$conf.int[1],condition.race.spending.90_pers1$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$race <- factor(rep(c("White\nRespondents","Black\nRespondents","Full\nSample"),each=2))
treatment_tab$Subset <- factor(rep(c("Don't Know Someone\nw/ Addiction","Know Someone\nw/ Addiction"),times=3))
treatment_tab$dv <- "Treatment Spending\n(interval scale)"


treatment_tab_punitive <- data.frame(
	rbind(
		c((condition.race.punitive.95_white_pers0$estimate[1]-condition.race.punitive.95_white_pers0$estimate[2]),condition.race.punitive.95_white_pers0$conf.int[1],condition.race.punitive.95_white_pers0$conf.int[2],condition.race.punitive.90_white_pers0$conf.int[1],condition.race.punitive.90_white_pers0$conf.int[2]),
		c((condition.race.punitive.95_white_pers1$estimate[1]-condition.race.punitive.95_white_pers1$estimate[2]),condition.race.punitive.95_white_pers1$conf.int[1],condition.race.punitive.95_white_pers1$conf.int[2],condition.race.punitive.90_white_pers1$conf.int[1],condition.race.punitive.90_white_pers1$conf.int[2]),
		
		c((condition.race.punitive.95_black_pers0$estimate[1]-condition.race.punitive.95_black_pers0$estimate[2]),condition.race.punitive.95_black_pers0$conf.int[1],condition.race.punitive.95_black_pers0$conf.int[2],condition.race.punitive.90_black_pers0$conf.int[1],condition.race.punitive.90_black_pers0$conf.int[2]),
		c((condition.race.punitive.95_black_pers1$estimate[1]-condition.race.punitive.95_black_pers1$estimate[2]),condition.race.punitive.95_black_pers1$conf.int[1],condition.race.punitive.95_black_pers1$conf.int[2],condition.race.punitive.90_black_pers1$conf.int[1],condition.race.punitive.90_black_pers1$conf.int[2]),
		
		c((condition.race.punitive.95_pers0$estimate[1]-condition.race.punitive.95_pers0$estimate[2]),condition.race.punitive.95_pers0$conf.int[1],condition.race.punitive.95_pers0$conf.int[2],condition.race.punitive.90_pers0$conf.int[1],condition.race.punitive.90_pers0$conf.int[2]),
		c((condition.race.punitive.95_pers1$estimate[1]-condition.race.punitive.95_pers1$estimate[2]),condition.race.punitive.95_pers1$conf.int[1],condition.race.punitive.95_pers1$conf.int[2],condition.race.punitive.90_pers1$conf.int[1],condition.race.punitive.90_pers1$conf.int[2])
	)
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$race <- factor(rep(c("White\nRespondents","Black\nRespondents","Full\nSample"),each=2))
treatment_tab_punitive$Subset <- factor(rep(c("Don't Know Someone\nw/ Addiction","Know Someone\nw/ Addiction"),times=3))
treatment_tab_punitive$dv <- "Enforcement Spending\n(interval scale)"

variables <- bind_rows(treatment_tab,treatment_tab_punitive)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)

(plot_treat <- ggplot(variables, aes(x=race,shape=Subset,group=Subset)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),position=position_dodge(width=0.25),
								width=.0, size=0.75) +
	geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),position=position_dodge(width=0.25),
								width=.0, size=1.5) +
	facet_wrap(facets = "dv",nrow = 1) + 
	geom_point(aes(y=treatment_effect),position=position_dodge(width=0.25),
						 size=4) + 
	geom_hline(aes(yintercept=0),lty=2) + 
	# xlab(NULL) +
	ylab("Treatment effect of recipient race\non spending support (black-white)") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.21,0.21)) +
	scale_x_discrete("",limits=c("White\nRespondents","Black\nRespondents","Full\nSample")) + 
	scale_shape_discrete("") + 
	coord_flip() +
	theme(text=element_text(colour=black,
													# family="CM Roman",
													size=15),
				legend.position="bottom")
)

# fname = "race_2dvs_treatment_resprace_pers.pdf"
fname = "FigureM24.pdf"
CairoPDF(fname, width=9, height=5)
print(plot_treat)
dev.off()


condition.gender.spending.90_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$personal_bi==0],conf.level = 0.9)
condition.gender.spending.95_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$personal_bi==0],conf.level = 0.95)
condition.gender.spending.90_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$personal_bi==1],conf.level = 0.9)
condition.gender.spending.95_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$personal_bi==1],conf.level = 0.95)
condition.gender.punitiive.90_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$personal_bi==0],conf.level = 0.9)
condition.gender.punitiive.95_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$personal_bi==0],conf.level = 0.95)
condition.gender.punitiive.90_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$personal_bi==1],conf.level = 0.9)
condition.gender.punitiive.95_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$personal_bi==1],conf.level = 0.95)

condition.gender.spending.90_female_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.gender.spending.95_female_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.gender.spending.90_female_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.gender.spending.95_female_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==1],conf.level = 0.95)
condition.gender.punitiive.90_female_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==0],conf.level = 0.9)
condition.gender.punitiive.95_female_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==0],conf.level = 0.95)
condition.gender.punitiive.90_female_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==1],conf.level = 0.9)
condition.gender.punitiive.95_female_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==1 & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==1 & a$personal_bi==1],conf.level = 0.95)


condition.gender.spending.90_male_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==0],conf.level = 0.9)
condition.gender.spending.95_male_pers0 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==0],a$spending[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==0],conf.level = 0.95)
condition.gender.spending.90_male_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==1],conf.level = 0.9)
condition.gender.spending.95_male_pers1 <- t.test(a$spending[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==1],a$spending[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==1],conf.level = 0.95)
condition.gender.punitiive.90_male_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==0],conf.level = 0.9)
condition.gender.punitiive.95_male_pers0 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==0],a$enforcement[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==0],conf.level = 0.95)
condition.gender.punitiive.90_male_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==1],conf.level = 0.9)
condition.gender.punitiive.95_male_pers1 <- t.test(a$enforcement[a$condition_gender=="Male" & a$female_resp==0 & a$personal_bi==1],a$enforcement[a$condition_gender=="Female" & a$female_resp==0 & a$personal_bi==1],conf.level = 0.95)

p_vals_figM25 <- c(condition.gender.spending.95_pers1$p.value, condition.gender.spending.95_pers0$p.value,
                   condition.gender.spending.95_male_pers1$p.value, condition.gender.spending.95_male_pers0$p.value, 
                   condition.gender.spending.95_female_pers1$p.value, condition.gender.spending.95_female_pers0$p.value,
                   condition.gender.punitiive.95_pers1$p.value, condition.gender.punitiive.95_pers0$p.value,
                   condition.gender.punitiive.95_male_pers1$p.value, condition.gender.punitiive.95_male_pers0$p.value, 
                   condition.gender.punitiive.95_female_pers1$p.value, condition.gender.punitiive.95_female_pers0$p.value)

treatment_tab <- data.frame(
	rbind(
		c((condition.gender.spending.95_female_pers0$estimate[1]-condition.gender.spending.95_female_pers0$estimate[2]),condition.gender.spending.95_female_pers0$conf.int[1],condition.gender.spending.95_female_pers0$conf.int[2],condition.gender.spending.90_female_pers0$conf.int[1],condition.gender.spending.90_female_pers0$conf.int[2]),
		c((condition.gender.spending.95_female_pers1$estimate[1]-condition.gender.spending.95_female_pers1$estimate[2]),condition.gender.spending.95_female_pers1$conf.int[1],condition.gender.spending.95_female_pers1$conf.int[2],condition.gender.spending.90_female_pers1$conf.int[1],condition.gender.spending.90_female_pers1$conf.int[2]),
		
		c((condition.gender.spending.95_male_pers0$estimate[1]-condition.gender.spending.95_male_pers0$estimate[2]),condition.gender.spending.95_male_pers0$conf.int[1],condition.gender.spending.95_male_pers0$conf.int[2],condition.gender.spending.90_male_pers0$conf.int[1],condition.gender.spending.90_male_pers0$conf.int[2]),
		c((condition.gender.spending.95_male_pers1$estimate[1]-condition.gender.spending.95_male_pers1$estimate[2]),condition.gender.spending.95_male_pers1$conf.int[1],condition.gender.spending.95_male_pers1$conf.int[2],condition.gender.spending.90_male_pers1$conf.int[1],condition.gender.spending.90_male_pers1$conf.int[2]),
		
		c((condition.gender.spending.95_pers0$estimate[1]-condition.gender.spending.95_pers0$estimate[2]),condition.gender.spending.95_pers0$conf.int[1],condition.gender.spending.95_pers0$conf.int[2],condition.gender.spending.90_pers0$conf.int[1],condition.gender.spending.90_pers0$conf.int[2]),
		c((condition.gender.spending.95_pers1$estimate[1]-condition.gender.spending.95_pers1$estimate[2]),condition.gender.spending.95_pers1$conf.int[1],condition.gender.spending.95_pers1$conf.int[2],condition.gender.spending.90_pers1$conf.int[1],condition.gender.spending.90_pers1$conf.int[2])
	)
)
names(treatment_tab) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab$gender <- factor(rep(c("Women\nRespondents","Men\nRespondents","Full\nSample"),each=2))
treatment_tab$Subset <- factor(rep(c("Don't Know Someone\nw/ Addiction","Know Someone\nw/ Addiction"),times=3))
treatment_tab$dv <- "Treatment Spending\n(interval scale)"


treatment_tab_punitive <- data.frame(
	rbind(
		c((condition.gender.punitiive.95_female_pers0$estimate[1]-condition.gender.punitiive.95_female_pers0$estimate[2]),condition.gender.punitiive.95_female_pers0$conf.int[1],condition.gender.punitiive.95_female_pers0$conf.int[2],condition.gender.punitiive.90_female_pers0$conf.int[1],condition.gender.punitiive.90_female_pers0$conf.int[2]),
		c((condition.gender.punitiive.95_female_pers1$estimate[1]-condition.gender.punitiive.95_female_pers1$estimate[2]),condition.gender.punitiive.95_female_pers1$conf.int[1],condition.gender.punitiive.95_female_pers1$conf.int[2],condition.gender.punitiive.90_female_pers1$conf.int[1],condition.gender.punitiive.90_female_pers1$conf.int[2]),
		
		c((condition.gender.punitiive.95_male_pers0$estimate[1]-condition.gender.punitiive.95_male_pers0$estimate[2]),condition.gender.punitiive.95_male_pers0$conf.int[1],condition.gender.punitiive.95_male_pers0$conf.int[2],condition.gender.punitiive.90_male_pers0$conf.int[1],condition.gender.punitiive.90_male_pers0$conf.int[2]),
		c((condition.gender.punitiive.95_male_pers1$estimate[1]-condition.gender.punitiive.95_male_pers1$estimate[2]),condition.gender.punitiive.95_male_pers1$conf.int[1],condition.gender.punitiive.95_male_pers1$conf.int[2],condition.gender.punitiive.90_male_pers1$conf.int[1],condition.gender.punitiive.90_male_pers1$conf.int[2]),
		
		c((condition.gender.punitiive.95_pers0$estimate[1]-condition.gender.punitiive.95_pers0$estimate[2]),condition.gender.punitiive.95_pers0$conf.int[1],condition.gender.punitiive.95_pers0$conf.int[2],condition.gender.punitiive.90_pers0$conf.int[1],condition.gender.punitiive.90_pers0$conf.int[2]),
		c((condition.gender.punitiive.95_pers1$estimate[1]-condition.gender.punitiive.95_pers1$estimate[2]),condition.gender.punitiive.95_pers1$conf.int[1],condition.gender.punitiive.95_pers1$conf.int[2],condition.gender.punitiive.90_pers1$conf.int[1],condition.gender.punitiive.90_pers1$conf.int[2])
	)
)
names(treatment_tab_punitive) <- c("treatment_effect","ci.lo.95","ci.hi.95","ci.lo.90","ci.hi.90")
treatment_tab_punitive$gender <- factor(rep(c("Women\nRespondents","Men\nRespondents","Full\nSample"),each=2))
treatment_tab_punitive$Subset <- factor(rep(c("Don't Know Someone\nw/ Addiction","Know Someone\nw/ Addiction"),times=3))
treatment_tab_punitive$dv <- "Enforcement Spending\n(interval scale)"

variables <- bind_rows(treatment_tab,treatment_tab_punitive)
variables
variables$dv <- factor(variables$dv,levels = c("Treatment Spending\n(interval scale)","Enforcement Spending\n(interval scale)"),ordered = T)


(plot_treat <- ggplot(variables, aes(x=gender,shape=Subset,group=Subset)) +
	geom_errorbar(aes(ymin=ci.lo.95, ymax=ci.hi.95),position=position_dodge(width=0.25),
								width=.0, size=0.75) +
	geom_errorbar(aes(ymin=ci.lo.90, ymax=ci.hi.90),position=position_dodge(width=0.25),
								width=.0, size=1.5) +
	facet_wrap(facets = "dv",nrow = 1) + 
	geom_point(aes(y=treatment_effect),position=position_dodge(width=0.25),
						 size=4) + 
	geom_hline(aes(yintercept=0),lty=2) + 
	# xlab(NULL) +
	ylab("Treatment effect of recipient gender\non spending support (male - female)") +
	theme_bw() + 
	scale_y_continuous(limits=c(-0.21,0.21)) +
	scale_x_discrete("",limits=c("Women\nRespondents","Men\nRespondents","Full\nSample")) + 
	scale_shape_discrete("") + 
	coord_flip() +
	theme(text=element_text(colour=black,
													# family="CM Roman",
													size=15),
				legend.position="bottom")
)

# fname = "gender_2dvs_treatment_respgender_pers.pdf"
fname = "FigureM25.pdf"
CairoPDF(fname, width=9, height=5)
print(plot_treat)
dev.off()




#### Mediation analyses ####
library(mediation)

#### continuous
med.treat.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                    + white_resp + female_resp + classification,
                    data = filter(a, !is.na(spending)))
out.treat.fit <- lm(spending ~ indiv_blame + condition_race + condition_gender + condition_location
                    + white_resp + female_resp + classification,
                    data = a)
med.pun.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                  + white_resp + female_resp + classification,
                  data = filter(a, !is.na(enforcement)))
out.pun.fit <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                  + white_resp + female_resp + classification,
                  data = a)

## treat - by race 
set.seed(02139)
med.treat.race.out <- mediate(med.treat.fit, out.treat.fit, 
                              treat = "condition_race", mediator = "indiv_blame",
                              control.value = "White", treat.value = "Black",
                              robustSE = TRUE, sims = 1000)
summary(med.treat.race.out) # ACME p = 0.19
# plot(med.treat.race.out)

# treat - white respondents
med.treat.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                    + female_resp + classification,
                    data = filter(a, !is.na(spending) & white_resp=="1"))
out.treat.fit <- lm(spending ~ indiv_blame + condition_race + condition_gender + condition_location
                    + female_resp + classification,
                    data = filter(a, white_resp=="1"))

set.seed(02139)
med.treat.white.out <- mediate(med.treat.fit, out.treat.fit, 
                               treat = "condition_race", mediator = "indiv_blame",
                               control.value = "White", treat.value = "Black",
                               robustSE = TRUE, sims = 1000)
summary(med.treat.white.out) # ACME -.000 p = 0.35, ADE of -.035 recovered
# plot(med.treat.white.out)

# treat - black respondents
med.treat.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                    + female_resp + classification,
                    data = filter(a, !is.na(spending) & black_resp=="1"))
out.treat.fit <- lm(spending ~ indiv_blame + condition_race + condition_gender + condition_location
                    + female_resp + classification,
                    data = filter(a, black_resp=="1"))

set.seed(02139)
med.treat.black.out <- mediate(med.treat.fit, out.treat.fit, 
                               treat = "condition_race", mediator = "indiv_blame",
                               control.value = "White", treat.value = "Black",
                               robustSE = TRUE, sims = 1000)
summary(med.treat.black.out) # ACME -.007 p = 0.18, ADE .093
# plot(med.treat.black.out)

# plot(med.treat.white.out,med.treat.black.out)

## punish - by race
set.seed(02139)
med.pun.race.out <- mediate(med.pun.fit, out.pun.fit,
                            treat = "condition_race", mediator = "indiv_blame",
                            control.value = "White", treat.value = "Black",
                            robustSE = TRUE, sims = 1000)
summary(med.pun.race.out) # ACME .003 p = 0.26, ADE -.017

# punish - white respondents
med.pun.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                  + female_resp + classification,
                  data = filter(a, !is.na(enforcement) & white_resp=="1"))
out.pun.fit <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                  + female_resp + classification,
                  data = filter(a, white_resp=="1"))

set.seed(02139)
med.pun.white.out <- mediate(med.pun.fit, out.pun.fit, 
                             treat = "condition_race", mediator = "indiv_blame",
                             control.value = "White", treat.value = "Black",
                             robustSE = TRUE, sims = 1000)
# plot(med.pun.white.out, main = "Punishment, White Respondents") # ACME .003 p = .41, ADE -.028

# punish - black respondents
med.pun.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                  + female_resp + classification,
                  data = filter(a, !is.na(enforcement) & black_resp=="1"))
out.pun.fit <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                  + female_resp + classification,
                  data = filter(a, black_resp=="1"))

set.seed(02139)
med.pun.black.out <- mediate(med.pun.fit, out.pun.fit, 
                             treat = "condition_race", mediator = "indiv_blame",
                             control.value = "White", treat.value = "Black",
                             robustSE = TRUE, sims = 1000)
# plot(med.pun.black.out) # ACME .005 p = 0.27, ADE .096

## Figure L29
# pdf(file = "mediation_race.pdf",
#     width = 6, # The width of the plot in inches
#     height = 5)
# par(mfrow=c(2,2))
# plot(med.treat.white.out, main = "Treatment\n White Respondents")
# plot(med.treat.black.out, main = "Treatment\n Black Respondents")
# plot(med.pun.white.out, main = "Enforcement\n White Respondents")
# plot(med.pun.black.out, main = "Enforcement\n Black Respondents")
# dev.off()
pdf(file = "FigureL21.pdf",
    width = 6, # The width of the plot in inches
    height = 5)
par(mfrow=c(2,2))
plot(med.treat.white.out, main = "Treatment\n White Respondents")
plot(med.treat.black.out, main = "Treatment\n Black Respondents")
plot(med.pun.white.out, main = "Enforcement\n White Respondents")
plot(med.pun.black.out, main = "Enforcement\n Black Respondents")
dev.off()


p_vals_figL21 <- c(summary(med.treat.white.out)$d.avg.p, summary(med.treat.black.out)$d.avg.p, 
                   summary(med.pun.white.out)$d.avg.p, summary(med.pun.black.out)$d.avg.p)

# gender
med.treat.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                    + white_resp + female_resp + classification,
                    data = filter(a, !is.na(spending)))
out.treat.fit <- lm(spending ~ indiv_blame + condition_race + condition_gender + condition_location
                    + white_resp + female_resp + classification,
                    data = a)
med.pun.fit <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                  + white_resp + female_resp + classification,
                  data = filter(a, !is.na(enforcement)))
out.pun.fit <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                  + white_resp + female_resp + classification,
                  data = a)
set.seed(02139)
med.treat.gender.out <- mediate(med.treat.fit, out.treat.fit, 
                                treat = "condition_gender", mediator = "indiv_blame",
                                control.value = "Female", treat.value = "Male",
                                robustSE = TRUE, sims = 1000)
summary(med.treat.gender.out) # AMCE p=0.56


set.seed(02139)
med.pun.gender.out <- mediate(med.pun.fit, out.pun.fit, 
                              treat = "condition_gender", mediator = "indiv_blame",
                              control.value = "Female", treat.value = "Male",
                              robustSE = TRUE, sims = 1000)
summary(med.pun.gender.out) # AMCE p=0.59

# gender - female
med.treat.fit.gender <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                           + white_resp + classification,
                           data = filter(a, !is.na(spending) & female_resp=="1"))
out.treat.fit.gender <- lm(spending ~ indiv_blame + condition_race + condition_gender+ condition_location
                           + white_resp + classification,
                           data = filter(a, female_resp=="1"))
med.pun.fit.gender <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                         + white_resp + classification,
                         data = filter(a, !is.na(enforcement) & female_resp=="1"))
out.pun.fit.gender <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                         + white_resp + classification,
                         data = filter(a, female_resp=="1"))

set.seed(02139)
med.treat.gender.female.out <- mediate(med.treat.fit.gender, out.treat.fit.gender,
                                       treat = "condition_gender", mediator = "indiv_blame",
                                       control.value = "Female", treat.value = "Male",
                                       robustSE = TRUE, sims = 1000)
summary(med.treat.gender.female.out) # AMCE p=0.43

set.seed(02139)
med.pun.gender.female.out <- mediate(med.pun.fit.gender, out.pun.fit.gender, 
                                     treat = "condition_gender", mediator = "indiv_blame",
                                     control.value = "Female", treat.value = "Male",
                                     robustSE = TRUE, sims = 1000)
summary(med.pun.gender.female.out) # AMCE p=0.46, ADE -.065

# gender - male
med.treat.fit.gender <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                           + white_resp + classification,
                           data = filter(a, !is.na(spending) & female_resp=="0"))
out.treat.fit.gender <- lm(spending ~ indiv_blame + condition_race + condition_gender + condition_location
                           + white_resp + classification,
                           data = filter(a, female_resp=="0"))
med.pun.fit.gender <- lm(indiv_blame ~ condition_race + condition_gender + condition_location
                         + white_resp + classification,
                         data = filter(a, !is.na(enforcement) & female_resp=="0"))
out.pun.fit.gender <- lm(enforcement ~ indiv_blame + condition_race + condition_gender + condition_location
                         + white_resp + classification,
                         data = filter(a, female_resp=="0"))

set.seed(02139)
med.treat.gender.male.out <- mediate(med.treat.fit.gender, out.treat.fit.gender,
                                     treat = "condition_gender", mediator = "indiv_blame",
                                     control.value = "Female", treat.value = "Male",
                                     robustSE = TRUE, sims = 1000)
summary(med.treat.gender.male.out) # AMCE p=1.00

set.seed(02139)
med.pun.gender.male.out <- mediate(med.pun.fit.gender, out.pun.fit.gender, 
                                   treat = "condition_gender", mediator = "indiv_blame",
                                   control.value = "Female", treat.value = "Male",
                                   robustSE = TRUE, sims = 1000)
summary(med.pun.gender.male.out) # AMCE p=0.98


## Figure L30
# pdf(file = "mediation_gender.pdf",
#     width = 6, # The width of the plot in inches
#     height = 5)
# par(mfrow=c(2,2))
# plot(med.treat.gender.female.out, main = "Treatment\n Female Respondents")
# plot(med.treat.gender.male.out, main = "Treatment\n Male Respondents")
# plot(med.pun.gender.female.out, main = "Enforcement\n Female Respondents")
# plot(med.pun.gender.male.out, main = "Enforcement\n Male Respondents")
# dev.off()
pdf(file = "FigureL22.pdf",
    width = 6, # The width of the plot in inches
    height = 5)
par(mfrow=c(2,2))
plot(med.treat.gender.female.out, main = "Treatment\n Female Respondents")
plot(med.treat.gender.male.out, main = "Treatment\n Male Respondents")
plot(med.pun.gender.female.out, main = "Enforcement\n Female Respondents")
plot(med.pun.gender.male.out, main = "Enforcement\n Male Respondents")
dev.off()


p_vals_figL22 <- c(summary(med.treat.gender.female.out)$d.avg.p, summary(med.treat.gender.male.out)$d.avg.p, 
                   summary(med.pun.gender.female.out)$d.avg.p, summary(med.pun.gender.male.out)$d.avg.p)




#### Power analyses ####
# install.packages("pwr")
# library(pwr)
# 
# spending.sd_black <- sd(a$spending[a$black_resp==1])
# spending_bi.sd_black <- sd(a$spending_bi[a$black_resp==1])
# spending.te_black <- mean(a$spending[which(a$condition_race=="Black" & a$black_resp==1)]) - mean(a$spending[which(a$condition_race=="White" & a$black_resp==1)])
# spending_bi.te_black <- mean(a$spending_bi[which(a$condition_race=="Black" & a$black_resp==1)]) - mean(a$spending_bi[which(a$condition_race=="White" & a$black_resp==1)])
# 
# spending.sd_white <- sd(a$spending[a$white_resp==1],na.rm=T)
# spending_bi.sd_white <- sd(a$spending_bi[a$white_resp==1],na.rm=T)
# spending.te_white <- mean(a$spending[which(a$condition_race=="Black" & a$white_resp==1)]) - mean(a$spending[which(a$condition_race=="White" & a$white_resp==1)])
# spending_bi.te_white <- mean(a$spending_bi[which(a$condition_race=="Black" & a$white_resp==1)]) - mean(a$spending_bi[which(a$condition_race=="White" & a$white_resp==1)])
# 
# # writeLines(as.character(round(
# # 	spending_bi.te_black,
# # 	2)),
# # 	"race_TE_pilot_blackresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.sd_black,
# # 	2)),
# # 	"spending_sd_pilot_blackresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.te_white,
# # 	2)),
# # 	"race_TE_pilot_whiteresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.sd_white,
# # 	2)),
# # 	"spending_sd_pilot_whiteresp.tex",sep = "")
# 
# ES.pilot_black <- abs(spending_bi.te_black/spending_bi.sd_black)
# ES.pilot_white <- abs(spending_bi.te_white/spending_bi.sd_white)
# 
# # writeLines(as.character(round(
# # 	ES.pilot_black,
# # 	2)),
# # 	"race_ES_pilot_black.tex",sep = "")
# # writeLines(as.character(round(
# # 	ES.pilot_white,
# # 	2)),
# # 	"race_ES_pilot_white.tex",sep = "")
# 
# 
# pilot.pwr_black <- pwr.t.test(d = ES.pilot_black, sig.level = 0.05, power = 0.8, alternative = "two.sided")
# pilot.pwr_black$n # N=118 for each treatment group
# pilot.pwr_white <- pwr.t.test(d = ES.pilot_white, sig.level = 0.05, power = 0.8, alternative = "two.sided")
# pilot.pwr_white$n # N=529 for each group
# 
# # writeLines(as.character(ceiling(
# # 	pilot.pwr_black$n)),
# # 	"race_pwrn_pilot_black.tex",sep = "")
# # writeLines(as.character(ceiling(
# # 	pilot.pwr_white$n)),
# # 	"race_pwrn_pilot_white.tex",sep = "")
# 
# # writeLines(as.character(ceiling(
# # 	pilot.pwr_white$n)*2 + ceiling(pilot.pwr_black$n*2)),
# # 	"race_pwrntotal_pilot.tex",sep = "")
# 
# spending.sd_rural <- sd(a$spending[a$classification=="rural"],na.rm=T)
# spending_bi.sd_rural <- sd(a$spending_bi[a$classification=="rural"],na.rm=T)
# spending.te_rural <- mean(a$spending[which(a$condition_context=="rural" & a$classification=="rural")]) - mean(a$spending[which(a$condition_context!="rural" & a$classification=="rural")])
# spending_bi.te_rural <- mean(a$spending_bi[which(a$condition_context=="rural" & a$classification=="rural")]) - mean(a$spending_bi[which(a$condition_context!="rural" & a$classification=="rural")])
# 
# spending.sd_nonrural <- sd(a$spending[a$classification!="rural"],na.rm=T)
# spending_bi.sd_nonrural <- sd(a$spending_bi[a$classification!="rural"],na.rm=T)
# spending.te_nonrural <- mean(a$spending[which(a$condition_context=="rural" & a$classification!="rural")]) - mean(a$spending[which(a$condition_context!="rural" & a$classification!="rural")])
# spending_bi.te_nonrural <- mean(a$spending_bi[which(a$condition_context=="rural" & a$classification!="rural")]) - mean(a$spending_bi[which(a$condition_context!="rural" & a$classification!="rural")])
# 
# # writeLines(as.character(round(
# # 	spending_bi.te_rural,
# # 	2)),
# # 	"context_TE_pilot_ruralresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.sd_rural,
# # 	2)),
# # 	"spending_sd_pilot_ruralresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.te_nonrural,
# # 	2)),
# # 	"context_TE_pilot_nonruralresp.tex",sep = "")
# # writeLines(as.character(round(
# # 	spending_bi.sd_nonrural,
# # 	2)),
# # 	"spending_sd_pilot_nonruralresp.tex",sep = "")
# 
# ES.pilot_rural <- abs(spending_bi.te_rural/spending_bi.sd_rural)
# ES.pilot_nonrural <- abs(spending_bi.te_nonrural/spending_bi.sd_nonrural)
# 
# # writeLines(as.character(round(
# # 	ES.pilot_rural,
# # 	2)),
# # 	"context_ES_pilot_rural.tex",sep = "")
# # writeLines(as.character(round(
# # 	ES.pilot_nonrural,
# # 	2)),
# # 	"context_ES_pilot_nonrural.tex",sep = "")
# 
# 
# pilot.pwr_rural <- pwr.t.test(d = ES.pilot_rural, sig.level = 0.05, power = 0.8, alternative = "two.sided")
# pilot.pwr_rural$n # N=145 for each treatment group
# pilot.pwr_nonrural <- pwr.t.test(d = ES.pilot_nonrural, sig.level = 0.05, power = 0.8, alternative = "two.sided")
# pilot.pwr_nonrural$n # N=4141 for each group

# writeLines(as.character(ceiling(
# 	pilot.pwr_rural$n)),
# 	"context_pwrn_pilot_rural.tex",sep = "")
# writeLines(as.character(ceiling(
# 	pilot.pwr_nonrural$n)),
# 	"context_pwrn_pilot_nonrural.tex",sep = "")
# 
# writeLines(as.character(ceiling(
# 	pilot.pwr_nonrural$n)*2 + ceiling(pilot.pwr_rural$n*2)),
# 	"context_pwrntotal_pilot.tex",sep = "")


#### Q value analysis ####

# if (!require("BiocManager", quietly = TRUE))
#   install.packages("BiocManager")
# BiocManager::install("qvalue")
library(qvalue)
library(xtable)
# main text
p_vals <- c(p_vals_fig2a, p_vals_fig2b, p_vals_fig3, p_vals_fig4, p_vals_fig5,
            p_vals_fig6, p_vals_fig7, p_vals_fig8, 
            p_vals_figG5a, p_vals_figG5b, p_vals_figG6, p_vals_figG7, p_vals_figG8,
            p_vals_figG9, p_vals_figH10a, p_vals_figH10b,
            p_vals_figI11a, p_vals_figI11b, p_vals_figI11c,
            p_vals_figI12a, p_vals_figI12b, p_vals_figI12c,
            p_vals_figI13a, p_vals_figI13b,
            p_vals_figI14a, p_vals_figI14b,
            p_vals_figI15a, p_vals_figI15b, p_vals_figI15c,
            p_vals_figI16a, p_vals_figI16b, p_vals_figI16c,
            p_vals_figI17a, p_vals_figI17b, p_vals_figI17c,
            p_vals_figI18a, p_vals_figI18b, p_vals_figI18c,
            p_vals_figJ19a, p_vals_figJ19b,
            p_vals_figJ20a, p_vals_figJ20b,
            p_vals_figL21,
            p_vals_figL22,
            p_vals_figM23, p_vals_figM24,
            p_vals_figM25)
p_vals <- unname(p_vals, force = FALSE)
qobj <- qvalue(p_vals)
qvalues <- qobj$qvalues


# create table 1
multi_test_tab <- data.frame(Figure = c(rep("Figure 2", 8), 
                                          rep("Figure 3", 4),
                                          rep("Figure 4", 4),
                                          rep("Figure 5", 18),
                                          rep("Figure 6", 6),
                                         rep("Figure 7", 2),
                                        rep("Figure 8", 12),
                                        rep("Figure G5", 8),
                                        rep("Figure G6", 4),
                                        rep("Figure G7", 4),
                                        rep("Figure G8", 18),
                                        rep("Figure G9", 6),
                                        rep("Figure H10", 8),
                                        rep("Figure I11", 6),
                                        rep("Figure I12", 6),
                                        rep("Figure I13", 2),
                                        rep("Figure I14", 2),
                                        rep("Figure I15", 18),
                                        rep("Figure I16", 18),
                                        rep("Figure I17", 18),
                                        rep("Figure I18", 18),
                                        rep("Figure J19", 8),
                                        rep("Figure J20", 8),
                                        rep("Figure L21", 4),
                                        rep("Figure L22", 4),
                                        rep("Figure M23", 12),
                                        rep("Figure M24", 12),
                                        rep("Figure M25", 12)),
                             Spending = c(rep("Treatment", 4), 
                                          rep("Enforcement", 4),
                                          rep("Treatment", 2),  # 3
                                          rep("Enforcement", 2),
                                          rep("Treatment", 2), # 4
                                          rep("Enforcement", 2),
                                          rep("Treatment", 9), # 5
                                          rep("Enforcement", 9),
                                          rep("Treatment", 3), # 6
                                          rep("Enforcement", 3),
                                          rep("Blame", 2), # 7
                                          rep("Treatment", 3), # 8 
                                          rep("Enforcement", 3),
                                          rep("Treatment", 3), 
                                          rep("Enforcement", 3),
                                          rep("Treatment", 4), 
                                          rep("Enforcement", 4),
                                          rep("Treatment", 2), # G14 
                                          rep("Enforcement", 2),
                                          rep("Treatment", 2), # G15
                                          rep("Enforcement", 2),
                                          rep("Treatment", 9), # G16
                                          rep("Enforcement", 9),
                                          rep("Treatment", 3), # G17
                                          rep("Enforcement", 3),
                                          rep("Treatment", 4), # H18
                                          rep("Enforcement", 4),
                                          rep("Treatment", 6), # I19
                                          rep("Enforcement", 6), # I20
                                          rep("Treatment", 2), # I21
                                          rep("Enforcement", 2), # I22
                                          rep("Treatment", 18), # I23
                                          rep("Enforcement", 18), # I24
                                          rep("Treatment", 18), # I25
                                          rep("Enforcement", 18), # I26
                                          rep("Treatment", 4), # J27
                                          rep("Enforcement", 4), 
                                          rep("Treatment", 4), # J28
                                          rep("Enforcement", 4), 
                                          rep("Treatment", 2), # L29
                                          rep("Enforcement", 2),
                                          rep("Treatment", 2), # L30
                                          rep("Enforcement", 2),
                                          rep("Treatment", 6), # M31
                                          rep("Enforcement", 6),
                                          rep("Treatment", 6), # M32 
                                          rep("Enforcement", 6), 
                                          rep("Treatment", 6), # M333
                                          rep("Enforcement", 6) # 
                                          ),
                               Hypothesis = c("Race",
                                      "Gender",
                                      "Context (suburban)", 
                                      "Context (rural)",
                             "Race",
                             "Gender",
                             "Context (suburban)", 
                             "Context (rural)",
                             "Race by respondent (Black)",
                             "Race by respondent (white)",
                             "Race by respondent (Black)",
                             "Race by respondent (white)",
                             "Gender by respondent (man)",
                             "Gender by respondent (woman)",
                             "Gender by respondent (man)",
                             "Gender by respondent (woman)",
                             "Context (sub. v. urban) by resp. (rural)",
                             "Context (rural v. urban) by resp. (rural)",
                             "Context (rural v. sub.) by resp. (rural)",
                             "Context (sub. v. urban) by resp. (sub.)",
                             "Context (rural v. urban) by resp. (sub.)",
                             "Context (rural v. sub.) by resp. (sub.)",
                             "Context (sub. v. urban) by resp. (urban)",
                             "Context (rural v. urban) by resp. (urban)",
                             "Context (rural v. sub.) by resp. (urban)",
                             "Context (sub. v. urban) by resp. (rural)",
                             "Context (rural v. urban) by resp. (rural)",
                             "Context (rural v. sub.) by resp. (rural)",
                             "Context (sub. v. urban) by resp. (sub.)",
                             "Context (rural v. urban) by resp. (sub.)",
                             "Context (rural v. sub.) by resp. (sub.)",
                             "Context (sub. v. urban) by resp. (urban)",
                             "Context (rural v. urban) by resp. (urban)",
                             "Context (rural v. sub.) by resp. (urban)",
                             "Race (matching)", 
                             "Gender (matching)",
                             "Context (matching)",
                             "Race (matching)",
                             "Gender (matching)",
                             "Context (matching",
                             "Illegal pills",
                             "Legal pills",
                             "Race (matching) x Heroin", 
                             "Gender (matching) x Heroin",
                             "Context (matching) x Heroin",
                             "Race (matching) x Heroin", 
                             "Gender (matching) x Heroin",
                             "Context (matching) x Heroin",
                             "Race (matching) x Legal pills", 
                             "Gender (matching) x Legal pills",
                             "Context (matching) x Legal pills",
                             "Race (matching) x Legal pills", 
                             "Gender (matching) x Legal pills",
                             "Context (matching) x Legal pills",
                             "Race", # G13
                             "Gender",
                             "Context (suburban)",
                             "Context (rural)",
                             "Race",
                             "Gender",
                             "Context (suburban)",
                             "Context (rural)",
                             "Race by respondent (Black)", # G14
                             "Race by respondent (white)",
                             "Race by respondent (Black)",
                             "Race by respondent (white)",
                             "Gender by respondent (man)", # G15
                             "Gender by respondent (woman)",
                             "Gender by respondent (man)",
                             "Gender by respondent (woman)",
                             "Context (sub. v. urban) by resp. (rural)", # G16
                             "Context (rural v. urban) by resp. (rural)",
                             "Context (rural v. sub.) by resp. (rural)",
                             "Context (sub. v. urban) by resp. (sub.)",
                             "Context (rural v. urban) by resp. (sub.)",
                             "Context (rural v. sub.) by resp. (sub.)",
                             "Context (sub. v. urban) by resp. (urban)",
                             "Context (rural v. urban) by resp. (urban)",
                             "Context (rural v. sub.) by resp. (urban)",
                             "Context (sub. v. urban) by resp. (rural)",
                             "Context (rural v. urban) by resp. (rural)",
                             "Context (rural v. sub.) by resp. (rural)",
                             "Context (sub. v. urban) by resp. (sub.)",
                             "Context (rural v. urban) by resp. (sub.)",
                             "Context (rural v. sub.) by resp. (sub.)",
                             "Context (sub. v. urban) by resp. (urban)",
                             "Context (rural v. urban) by resp. (urban)",
                             "Context (rural v. sub.) by resp. (urban)",
                             "Race (matching)", # G17
                             "Gender (matching)",
                             "Context (matching)",
                             "Race (matching)",
                             "Gender (matching)",
                             "Context (matching)",
                             "Insurance (ACA)", # H18
                             "Insurance (Private)",
                             "Pathway (Illegal pills)",
                             "Pathway (Legal pills)",
                             "Insurance (ACA)", 
                             "Insurance (Private)",
                             "Pathway (Illegal pills)",
                             "Pathway (Legal pills)",
                             "Race x Urban", # I19
                             "Gender x Urban", 
                             "Race x Suburban", 
                             "Gender x Suburban", 
                             "Race x Rural",
                             "Gender x Rural",
                             "Race x Urban", # I20
                             "Gender x Urban", 
                             "Race x Suburban", 
                             "Gender x Suburban", 
                             "Race x Rural",
                             "Gender x Rural",
                             "Race x Man", # I21
                             "Race x Woman",
                             "Race x Man", # I22
                             "Race x Woman",
                             "Race x Heroin", # I23
                             "Gender x Heroin",
                             "Context (suburban) x Heroin",
                             "Context (rural) x Heroin",
                             "Insurance (ACA) x Heroin",
                             "Insurance (private) x Heroin",
                             "Race x Illegal pills", 
                             "Gender x Illegal pills",
                             "Context (suburban) x Illegal pills",
                             "Context (rural) x Illegal pills",
                             "Insurance (ACA) x Illegal pills",
                             "Insurance (private) x Illegal pills",
                             "Race x Legal pills", 
                             "Gender x Legal pills",
                             "Context (suburban) x Legal pills",
                             "Context (rural) x Legal pills",
                             "Insurance (ACA) x Legal pills",
                             "Insurance (private) x Legal pills",
                             "Race x Heroin", # I24
                             "Gender x Heroin",
                             "Context (suburban) x Heroin",
                             "Context (rural) x Heroin",
                             "Insurance (ACA) x Heroin",
                             "Insurance (private) x Heroin",
                             "Race x Illegal pills", 
                             "Gender x Illegal pills",
                             "Context (suburban) x Illegal pills",
                             "Context (rural) x Illegal pills",
                             "Insurance (ACA) x Illegal pills",
                             "Insurance (private) x Illegal pills",
                             "Race x Legal pills", 
                             "Gender x Legal pills",
                             "Context (suburban) x Legal pills",
                             "Context (rural) x Legal pills",
                             "Insurance (ACA) x Legal pills",
                             "Insurance (private) x Legal pills",
                             "Race x Medicaid", # I25
                             "Gender x Medicaid",
                             "Context (suburban) x Medicaid",
                             "Context (rural) x Medicaid",
                             "Pathway (illegal pills) x Medicaid",
                             "Pathway (legal pills) x Medicaid",
                             "Race x ACA", 
                             "Gender x ACA",
                             "Context (suburban) x ACA",
                             "Context (rural) x ACA",
                             "Pathway (illegal pills) x ACA",
                             "Pathway (legal pills) x ACA",
                             "Race x Private", 
                             "Gender x Private",
                             "Context (suburban) x Private",
                             "Context (rural) x Private",
                             "Pathway (illegal pills) x Private",
                             "Pathway (legal pills) x Private",
                             "Race x Medicaid", # I26
                             "Gender x Medicaid",
                             "Context (suburban) x Medicaid",
                             "Context (rural) x Medicaid",
                             "Pathway (illegal pills) x Medicaid",
                             "Pathway (legal pills) x Medicaid",
                             "Race x ACA", 
                             "Gender x ACA",
                             "Context (suburban) x ACA",
                             "Context (rural) x ACA",
                             "Pathway (illegal pills) x ACA",
                             "Pathway (legal pills) x ACA",
                             "Race x Private", 
                             "Gender x Private",
                             "Context (suburban) x Private",
                             "Context (rural) x Private",
                             "Pathway (illegal pills) x Private",
                             "Pathway (legal pills) x Private",
                             "Race, Democrats", # J27
                             "Gender, Democrats", 
                             "Context (suburban), Democrats",
                             "Context (rural), Democrats",
                             "Race, Democrats", 
                             "Gender, Democrats", 
                             "Context (suburban), Democrats",
                             "Context (rural), Democrats",
                             "Race, Republicans", # J28
                             "Gender, Republicans", 
                             "Context (suburban), Republicans",
                             "Context (rural), Republicans",
                             "Race, Republicans", 
                             "Gender, Republicans", 
                             "Context (suburban), Republicans",
                             "Context (rural), Republicans",
                             "ACME (white)", # L29
                             "ACME (Black)",
                             "ACME (white)",
                             "ACME (Black)",
                             "ACME (woman)", # L30
                             "ACME (man)",
                             "ACME (woman)",
                             "ACME (man)",
                             "Race (matching), exposure", # M31
                             "Race (matching), no exposure",
                             "Gender (matching), exposure",
                             "Gender (matching), no exposure",
                             "Context (matching), exposure",
                             "Context (matching), no exposure",
                             "Race (matching), exposure",
                             "Race (matching), no exposure",
                             "Gender (matching), exposure",
                             "Gender (matching), no exposure",
                             "Context (matching), exposure",
                             "Context (matching), no exposure",
                             "Race, exposure", # M32
                             "Race, no exposure",
                             "Race by respondent (Black), exposure", 
                             "Race by respondent (Black), no exposure",
                             "Race by respondent (white), exposure",
                             "Race by respondent (white), no exposure",
                             "Race, exposure", 
                             "Race, no exposure",
                             "Race by respondent (Black), exposure", 
                             "Race by respondent (Black), no exposure",
                             "Race by respondent (white), exposure",
                             "Race by respondent (white), no exposure",
                             "Gender, exposure", # M33
                             "Gender, no exposure", 
                             "Gender by respondent (man), exposure", 
                             "Gender by respondent (man), no exposure", 
                             "Gender by respondent (woman), exposure",
                             "Gender by respondent (woman), no exposure",
                             "Gender, exposure", 
                             "Gender, no exposure", 
                             "Gender by respondent (man), exposure", 
                             "Gender by respondent (man), no exposure", 
                             "Gender by respondent (woman), exposure",
                             "Gender by respondent (woman), no exposure"),
                             'P-value' = p_vals[1:250],
                             'Q-value' = qvalues[1:250])
names(multi_test_tab) <- c("Figure", "Spending DV", "Hypothesis", "P-Value", "Q-Value")

## Table K2
print(xtable(multi_test_tab, digits = 2, 
             caption = "Multiple hypothesis testing corrections using q-values", 
             label = "tab:qvalues"), 
      include.rownames=FALSE,
      hline.after = c(-1, 0, 8, 12, 16, 34, 40, 42, 54, 62, 66, 70,
                      88, 94, 102, 108, 114, 116, 118, 136, 154,
                       172, 190, 198, 206, 210, 214, 226, 238, nrow(multi_test_tab)),
      floating=F,tabular.environment="longtable",
      # file="qvalue_table.tex"
      file="TableK2.tex"
)



